Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a Python class definition assign a closure variable to itself?

Why doesn't the following work in Python?

def make_class(a):
    class A(object):
        a=a
    return A
like image 469
joeforker Avatar asked Feb 21 '26 09:02

joeforker


1 Answers

works just fine:

>>> def make_class(a):
    class A(object):
        _a=a
    return A

>>> make_class('df')
<class '__main__.A'>
>>> make_class('df')._a
'df'

btw, function is not a reserved keyword in Python.

like image 60
SilentGhost Avatar answered Feb 23 '26 21:02

SilentGhost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!