Home > OS >  Default value with the same name as property
Default value with the same name as property

Time:09-08

I got a very strange behavior, where I can't use global variable as default value if a property has the same name, e.g.:

import datetime as dt


class A:
    @property
    def dt(self):
        return 1

    def foo(self, dt_=dt.datetime.min):
        print(dt_ > dt.datetime.now())
        pass

This gives an error:

Traceback (most recent call last):
  File "tmp.py", line 4, in <module>
    class A:
  File "tmp.py", line 9, in A
    def foo(self, dt_=dt.datetime.min):
AttributeError: 'property' object has no attribute 'datetime'

This seems counter-intuitive to me, why would a property override a global variable, but only in default assignment? dt works properly inside the method (can check by using a different default value).

What am I missing?

I checked on clean env of 3.8 and 3.10, so it doesn't seem to be a test version issue. I use python provided by conda.

CodePudding user response:

The class definition block creates a "temporary" scope, and the default arguments of methods are evaluated within this scope (which ceases to exist once the class has been defined).

Actually, the property is not necessary, here is a simpler way to demonstrate the same behavior:

>>> x = "val_outer"
>>> class A:
...     x = "val_class"
...     def foo(self, k=x):
...         print(k)
...         print(x)
... 
>>> A().foo()
val_class
val_outer

As mentioned in other answers linked in the comments, a reference for the weird class block scoping is in the execution model docs, section 4.2.2 Resolution of names:

Class definition blocks and arguments to exec() and eval() are special in the context of name resolution. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace. The namespace of the class definition becomes the attribute dictionary of the class. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope.

  • Related