Kay Schluehr wrote: > > Tomi Lindberg wrote: >> Hi, >> >> With the following function definition, is it possible to >> create an instance of class C outside the function f (and if >> it is, how)? > > def f(): > class C(object): > def __init__(self): > self.a = 'a' > f.C = C > return C() > >>>> f.C > <class '__main__.C'>
Not working, unless f has been called at least once. But what I didn't know (and always wondered) if the classdefinition inside a function can use the outer scope - and apparently, it can! Cool. def f(baseclass): class C(baseclass): def __init__(self): self.a = 'a' f.C = C def foo(self): print baseclass return C() c = f(object) print f.C c.foo() Diez -- http://mail.python.org/mailman/listinfo/python-list