[Tutor] List of class instances
Hi, I'd like to form a list of class instances. The following does not work (TextfieldLong, Textarea, TextfieldShort etc being class names): fields = [ TextfieldLong(name='title', label='Seitentitel', value=''), Textarea(name='content', label='Inhalt', value=''), ShortField(name='mother_id', label='MotherID', value=1) ] Is there a way to create such a list? Thanks in advance, Jan -- Any sufficiently advanced technology is indistinguishable from a Perl script. - Programming Perl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of class instances
Hi, Jan Eden wrote on 20.09.2005: Hi, I'd like to form a list of class instances. The following does not work (TextfieldLong, Textarea, TextfieldShort etc being class names): fields = [ TextfieldLong(name='title', label='Seitentitel', value=''), Textarea(name='content', label='Inhalt', value=''), ShortField(name='mother_id', label='MotherID', value=1) ] Just found that it *does* work, but that I have to define the classes above the list assignment. Why is that? Why would Python not find the classes within the same file? TIA, Jan -- Life's unfair - but root password helps! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of class instances
Jan Eden wrote: I'd like to form a list of class instances. The following does not work (TextfieldLong, Textarea, TextfieldShort etc being class names): fields = [ TextfieldLong(name='title', label='Seitentitel', value=''), Textarea(name='content', label='Inhalt', value=''), ShortField(name='mother_id', label='MotherID', value=1) ] Just found that it *does* work, but that I have to define the classes above the list assignment. Why is that? Why would Python not find the classes within the same file? Class definitions are executable statements that bind the name of the class to a class object. Before the definition is executed the class is undefined, like any other assignment. For example you wouldn't expect this to work: values = [x, y, z] x=1 y=2 z=3 Your code suffers from the same problem. When you say class foo: pass this means, roughly, foo = (the result of executing the class body) and the name 'foo' is not defined until this executes. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of class instances
Hi, Orri Ganel wrote on 20.09.2005: As a side-note, unless you're okay with only being able to access those instance variables through the fields list (ie fields[0], fields[1], fields[2]), you may want to actually name them first. Yes, I am fine with that - I actually prefer to have a sorted list instead of a dictionary. I'll always access them like for field in fields: ... Thanks, Jan -- I'd never join any club that would have the likes of me as a member. - Groucho Marx ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor