instantiate a 'classobj'

Steve Holden steve at holdenweb.com
Thu Jul 26 23:00:38 EDT 2007


westymatt wrote:
> I have a class where a parameter to its constructor is a type(param) =
> 'classobj'.  How would I go about instantiating that given it has no
> constructor.
> 
Just call the parameter: if it's of type classobj then it's callable, 
and calling it will create an instance of the class.

 >>> class MyClass:
...   def __init__(self, a, b):
...     self.a = a
...     self.b = b
...
 >>> MyClass
<class __main__.MyClass at 0x7ff1ac5c>
 >>> type(MyClass)
<type 'classobj'>
 >>> def builder(c):
...   return c(3, 4)
...
 >>> builder(MyClass)
<__main__.MyClass instance at 0x7ff281cc>
 >>>

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list