classes + modules

Peter Hansen peter at engcorp.com
Tue Apr 16 01:18:33 EDT 2002


Adonis Vargas wrote:
> 
> is there any way to initialize a class then get the initialized class name
> and put it into a list then later on calling it from the list as if it were
> the class?
> 
> class_list = []
> from somemodule import *
> x = someclass()
> class_list.append(x)
> class_list[0].somefunction() # assuming x is position at 0

Hey, come on, this is _Python_, man!  Try it!  :-)

C:\>python
Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class SomeClass:
...   def someMethod(self):
...     print "It works!"
...
>>> classList = []
>>> x = SomeClass()
>>> classList.append(x)
>>> classList[0].someMethod()
It works!


-Peter



More information about the Python-list mailing list