Search code examples
pythonclass-attributes

Class attributes dependent on other class attributes


I want to create a class attribute, that are dependent to another class attribute (and I tell class attribute, not instance attribute). When this class attribute is a string, as in this topic, the proposed solution

class A:
    foo = "foo"
    bar = foo[::-1]

print(A.bar)

works fine. But when the class attribute is a list or a tuple, as in my case, the following code dose not work...

x=tuple('nice cup of tea')

class A:
  remove = ('c','o','a',' ')
  remains = tuple(c for c in x if not c in remove)

print(A.remains)

raise

Traceback (most recent call last):
  File "[***.py]", line 3, in <module>
    class A:
  File "[***.py]", line 5, in A
    remains = tuple(c for c in x if not c in remove)
  File "[***.py]", line 5, in <genexpr>
    remains = tuple(c for c in x if not c in remove)
NameError: name 'remove' is not defined

Why this kind of methods works if my class attributes is less complex (as simple string, as in the mentioned previous topic) but not for tuple?

After investigations, I found this way:

x=tuple('nice cup of tea')

def sub(a,b):
   return tuple(c for c in a if not c in b)

class A:
  remove = ('c','o','a',' ')
  remains = sub(x, remove)

print(A.remains)

that works, but does not suit me, for these reasons:

  • I don't understand why this one, via an intermediary function, works and not without.
  • I don't want to add a function for just a single line with an elementary operation.

Solution

  • Python is trying to look up remove in the global scope, but it doesn't exist there. x, on the other hand, is looked up in the enclosing (class) scope.

    See the documentation:

    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. This means that the following will fail:

    class A:
        a = 42
        b = list(a + i for i in range(10))
    

    Displays for lists, sets and dictionaries

    The iterable expression in the leftmost for clause is evaluated directly in the enclosing scope and then passed as an argument to the implicitly nested scope. Subsequent for clauses and any filter condition in the leftmost for clause cannot be evaluated in the enclosing scope as they may depend on the values obtained from the leftmost iterable.