Is their an expression to create a class?

Chris Rebert clp2 at rebertia.com
Tue Mar 17 17:26:58 EDT 2009


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
-- 
I have a blog:
http://blog.rebertia.com



More information about the Python-list mailing list