4

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.
wjandrea
  • 28,235
  • 9
  • 60
  • 81
lg53
  • 121
  • 7
  • You could use a `lambda` for `sub`. – Scott Hunter Nov 09 '22 at 15:47
  • @ScottHunter: Yes for sure, but this remains an additional function – lg53 Nov 09 '22 at 15:49
  • Doesn't solve the problem, but this one works too: `remains = tuple(itertools.filterfalse(remove.__contains__, x))`. I really wonder what's going on there. – Matthias Nov 09 '22 at 16:01
  • It is worth noting that the issue doesn't seem to be related to the attribute type: given `foo = (1,2,3)`, `bar = foo[::-1]` will work, as will work `bar = tuple(x**2 for x in foo)` – gimix Nov 09 '22 at 16:05
  • 1
    It gets stranger and stranger. This one works: `remains = [c for c in remove]`. This one not: `remains = [c for c in remove if c in remove]`. It might be a bug. – Matthias Nov 09 '22 at 16:07
  • 3
    I created an [issue](https://github.com/python/cpython/issues/99295) on the Python bug tracker. – Matthias Nov 09 '22 at 16:48
  • I got the same behaviour with the current version of pypy. – Matthias Nov 09 '22 at 17:01
  • in vscode Pylance already complains about remove being undefined so the grammar expectations seems to be finding issue with it. i know list comprehensions have weird namescoping rules maybe that's causing the issue. – JL Peyret Nov 09 '22 at 19:44
  • 1
    I added a bunch of other cases to check it out. I've narrowed it down to the variable **not being seen if it's in the IF clause** of a comprehension. it's fine in the main part. For example, `remains = [(a,b) for a,b in zip(x,remove)]` is fine. but anything with `if remove` fails. The exact type of remove doesnt matter, nor the type of comprehension, only the location in the IF. https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions did not show anything special about the IF but ... the runtime fails, Pylance flags it and so does mypy. – JL Peyret Nov 09 '22 at 20:11
  • @Matthias I doubt it's a bug. runtime, mypy, Pylance all know to expect a problem. Consider putting the bug on hold until we know more - the core devs job is not to be answering SO questions (although maybe the docs need to be more detailed). I do wonder what Pypy makes of it. Edit: just read the bug and they point to https://docs.python.org/3/reference/executionmodel.html#naming-and-binding - that I am not sure I follow - the class-level variable lookup works fine in most cases, just not in the IF clause of comprehensions. – JL Peyret Nov 09 '22 at 20:19
  • The issue was closed already, but I'm with @JLPeyret here: I'm not quite pleased with the explanation. – Matthias Nov 09 '22 at 20:50
  • 1
    The full explanation is here: https://discuss.python.org/t/nameerror-in-class-definition-using-list-comprehension-with-if-statement/20913/1 . I'm still not happy with this behaviour but it is documented. – Matthias Nov 09 '22 at 21:32
  • FWIW, the only other solution I can think of off the top of my head is a lambda with a closure, but it's ugly as heck: `remains = (lambda r=remove: tuple(c for c in x if not c in r))()` – wjandrea Nov 09 '22 at 21:48
  • 1
    Long story short, list comprehensions use a function scope. But **class bodies do not create enclosing scopes**. This is why you cannot access a class variable in a method without `self` (this is an explicit design decision regarding python). So, just replace your list comprehension with a regular for-loop. – juanpa.arrivillaga Nov 09 '22 at 23:42
  • 3
    @JLPeyret this is not a bug, this is a well-known issue with trying to use comprehension constructs in class bodies. See my comment above. And this is a problem *everywhere* in the comprehension (not just the if clause) **except** the left-most for-clause, because that is evaluated in the scope of the caller. You can imagine `result = [x + y for x in for y in ]` to be transformed into `result = _inaccesible_list_comp_function()` – juanpa.arrivillaga Nov 09 '22 at 23:43
  • 1
    Alternatively, just do `A.bar = tuple(c for c in x if not c in A.remove)` after the end of the class definition statement. But really, the **best** solution is the one you already came up with, using a helper function. Just do that. That is a sane and reasonable way to accomplish this. – juanpa.arrivillaga Nov 09 '22 at 23:49

1 Answers1

5

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.

wjandrea
  • 28,235
  • 9
  • 60
  • 81