Class definition within function

Rick Zantow rzantow at gmail.com
Wed Aug 2 11:09:46 EDT 2006


Duncan Booth <duncan.booth at invalid.invalid> wrote in 
news:Xns9813882C9C0FDduncanbooth at 127.0.0.1:

>> >>> def f():
>>      class C(object):
>>           def __init__(self):
>>                self.a = 'a'
>>      return C()
>> 
>> >>> x = f()
>> >>> x.a
>> 'a'
>> >>> y=f.C()
>> 
> 

Of course there's this:

>>> def f():
...  class C(object):
...       def __init__(self):
...            self.a = 'a'
...  return C()
...
>>> x = f()
>>> x.a
'a'
>>> y=x.__class__()
>>> y.a
'a'
>>> type(y) == type(x)
True




More information about the Python-list mailing list