Class definition within function

Kay Schluehr kay.schluehr at gmx.net
Wed Aug 2 08:10:36 EDT 2006


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'>

> And yes, I think this is one of those times
> when the real question is why :)

Definitely ;)




More information about the Python-list mailing list