bug with isinstance() ?

John Machin sjmachin at lexicon.net
Wed Jun 1 19:28:23 EDT 2005


Mac wrote:
[snip]
> I guess the problem is I'm thinking of "classes"
> as these abstract concepts, sort of like Platonian "forms", whereas I
> should be thinking of classes as "class objects", object instances,
> each coming from some module's namespace...  is this sort of the idea?
> Someone help me wrap my head around this, please. :)
> 

Yes, you're getting it; Python is a dynamic language. Even classes can 
be created on the fly.

I have a kit for reading/writing boring old legacy mainframe-style files 
containg multiple record types with fixed-length fields. The record 
definitions are stored as data, not as code. A skeleton class's source 
is actually *local* to the class-making function. This means the class 
statement is executed each time the function is called. The function 
takes the current class instance like a shopping trolley and fills it up 
with more methods and attributes, before parking it in a dictionary 
keyed on the record name.

Taxpayer = file_dict['TXPYR']
taxpayer = Taxpayer()
taxpayer.name = 'John Q Doe'
taxpayer.date_of_birth = datetime.date(1970, 12, 31)
taxpayer.reportable_income = 1234.56
outfile.write(taxpayer.as_fixed_string())


Cheers,
John



More information about the Python-list mailing list