0

Is it appropriate to use a class variable accessed with self as a loop variable (aka loop counter, target_list, or iterated item)? I have not found a standard which describes this situation. The following python code is valid and produces the expected result ("1, 2, 3") but it feels wrong somehow:

class myClass():
    foo = None
    def myfunc(self):
        for self.foo in [1, 2, 3]:
            print(self.foo)
        return

myClass().myfunc()

I have not found a definitive answer that suggests using self.foo would be considered good or bad programming.

More specifically:

  • Does the MWE pass muster for standards?
  • Is the MWE behaving like I claim or is something else happening here?
wjandrea
  • 28,235
  • 9
  • 60
  • 81
WesH
  • 460
  • 5
  • 15
  • 1
    you are not actually assigning anything to `self.foo` in this situation, so it cannot be accessed by any other function in the class. – It_is_Chris Dec 15 '22 at 19:30
  • 2
    You never access the class attribute `myClass.foo`. You create a new instance attribute `foo`. `[1, 2, 3]` is the iterable in the loop, not `self.foo`. – Matthias Dec 15 '22 at 19:32
  • 3
    @It_is_Chris Yes, you are. The loop repeatedly assigns a value from the list to the `foo` attribute of the *instance*. (You can't assign to a class attribute via an instance of the class in any context.) The main issue here is that no reference to the instance of `myClass` is saved, so you there's no way to call another method on the instance after `myfunc` returns. The fact that any output at all is produced confirms that the assignment takes place, though. – chepner Dec 15 '22 at 19:46
  • For reference, [iterable](https://docs.python.org/3/glossary.html#term-iterable) is defined in the official Python glossary. – wjandrea Dec 15 '22 at 21:55
  • I'm not sure why the question was closed for being opinion-based. The comments have responded with factual reasons that the MWE is not behaving like I think and the question solicits references to why things should be done one way or another should such information exist in a standard somewhere. I'm going to try to reword to make this explicit and reopen. – WesH Dec 16 '22 at 21:16
  • @wjandrea - I see what you mean. I'm going to substitute the term "item" in my rework of the question unless you can suggest otherwise. – WesH Dec 16 '22 at 21:18
  • @WesH I think I'd use "loop variable", personally. The official term in the syntax is "target", which is generic. – wjandrea Dec 16 '22 at 21:22
  • @wjandrea Can you please cite your source for "target" or "loop variable" here? I would be happy to re-edit the question to improve the language and my own knowledge. I based "item" on the description from `__next__` which states "Return the next item from the iterator. If there are no further items [...]" as seen [here](https://docs.python.org/3/library/stdtypes.html#iterator.__next__). – WesH Dec 16 '22 at 21:26
  • @WesH In the official syntax, [the `for` statement](https://docs.python.org/3/reference/compound_stmts.html#the-for-statement) takes a *target_list* composed of *target*s. I don't have a source for "loop variable", but the Wikipedia page [for loop](https://en.wikipedia.org/wiki/For_loop) uses it. – wjandrea Dec 16 '22 at 21:31
  • @wjandrea I added more detail with citations in the edited version. Now here's hoping more folks vote to reopen it. – WesH Dec 16 '22 at 21:38
  • @WesH The question was closed because ["good practice" is poorly-defined](//meta.stackoverflow.com/q/296542/4518341), plus where it's conflating the class attribute with the instance attribute, it's not clear which aspect it's really asking about. – wjandrea Dec 16 '22 at 22:06
  • You might consider posting a new question after what you've learned, like, *'If I assign an attribute to "self", does it overwrite a class attribute with the same name?'* although there's probably an existing question like that, for example [this is similar](/q/63436006/4518341). You could also consider, *"Is it good practice to use an attribute as a loop variable?"* or *"Is it good practice to shadow a class attribute with an instance attribute?"* but those'd probably be too opinion-based too. – wjandrea Dec 16 '22 at 22:06

0 Answers0