Parsing & question about usages off classes!

Alex Martelli aleax at aleax.it
Sun Nov 3 12:21:18 EST 2002


Frans wrote:

> Hi,
> 
> I want to put each line off a logfile in a different class. But I cant
> seem to, dynamicly, create new classes. I am having problems using
> variables when creating new classes.
> 
> class TEST:
> pass
> 
> x=0
> while x < 10:
> A + "" + x = TEST()
> x=x+1
> 
> Something like that is what I want :)
> 
> Having A1 A2 A3 A4 etc. etc. as TEST classes.

Calling TEST() creates a new instance of the existing class
named TEST.  Of course, you cannot then assign that instance
to a string concatenation (you cannot assign ANYTHING to a
string concatenation), much less one that is invalid in
several ways (name A is not defined, x is an integer and
not a string, and the empty string in-between the attempted
concatenation makes absolutely no sense).

It's thus very hard to guess at what you're doing.

To create a new class, with a name computed dynamically at
runtime, you can use function classobj from module new in
the standard Python library.  That is UTTERLY DIFFERENT from
what you seem to be attempting to be doing in the above
snippet, so I don't know if the "something like that" spec
is even vaguely satisfied thereby.

If by any chance your request for creating new classes
has nothing to do with creating new classes but is rather
about binding dynamically computed names to new instances
of an existing class, my earnest advice is not to do that.
Use dictionaries and lists, NOT variables with dynamically
determined names -- the latter is never a good approach.


> Anyway way to do this ?

Creating new classes is easy, yes.  But what I suspect you
actually may want to do is completely different and utterly
unadvisable.  1 Corintians 6:12.


Alex




More information about the Python-list mailing list