Is their an expression to create a class?

Donald 'Paddy' McCarthy paddy3118 at netscape.net
Tue Mar 17 22:43:11 CET 2009


Chris Rebert wrote:
> On Tue, Mar 17, 2009 at 2:24 PM, Robert Kern <robert.kern at gmail.com> wrote:
>> On 2009-03-17 16:13, Paddy wrote:
>>> We the def statement and the lambda expression. We have the class
>>> statement, but is their an expression to create a class?
>>>
>>> Or:
>>>
>>>>>> def F(): pass
>>>>>> type(F)
>>> <type 'function'>
>>>>>> # Is to:
>>>>>> F2 = lambda : none
>>>>>> type(F2)
>>> <type 'function'>
>>>>>> # As
>>>>>> class O(object): pass
>>>>>> type(O)
>>> <type 'type'>
>>>>>> # is to:
>>>>>> # ????
>> type('O', (object,), {})
> 
> Further detail from the docs (http://docs.python.org/library/functions.html):
> 
> type(name, bases, dict)
> 
>     Return a new type object. This is essentially a dynamic form of
> the class statement. The name string is the class name and becomes the
> __name__ attribute; the bases tuple itemizes the base classes and
> becomes the __bases__ attribute; and the dict dictionary is the
> namespace containing definitions for class body and becomes the
> __dict__ attribute. For example, the following two statements create
> identical type objects:
> 
>     >>> class X(object):
>     ...     a = 1
>     ...
>     >>> X = type('X', (object,), dict(a=1))
> 
>     New in version 2.2.
> 
> Cheers,
> Chris


Thanks guys. Youve put my mind at rest!

- Paddy.



More information about the Python-list mailing list