flow
on its own is just a local variable, that lives as long as this method and then goes away.
self.flow
is an instance variable. That's what you wanted.
(If you're coming from another language like C++ that has an implicit this
/self
for members... Python doesn't have that. Guido explains why, even with some clever ideas that make the historical reasons no longer relevant, Python still doesn't have that, and probably never will.)
As a side note, in Python 2.7, always use class A(object):
, not class A:
. Otherwise, you're creating old-style classes. That doesn't make a difference in this case, but in cases where it does make a difference, you always want the new behavior.*
Looking around, I got the impression that perhaps the member variable might have to be initialized in init() during object construction phase, but that didn't help either.
Well, first, it's __init__
; a method just called init
won't get called during object construction.
Anyway, usually you want to create all your attributes in __init__
. (That's one easy way to guarantee that all instances of your type will have those members—unless you del
them, of course…) But there's nothing requiring you to. In Python, you can add and delete attributes to an object at any time.**
* There are some cases where arguably you don't, but if you don't already know those cases and how to evaluated those arguments, they don't apply to you.
** Unless the class prevents you from doing so. Many builtin types do; you can do something similar yourself with __slots__
; or you can even write a custom __setattr__
method that does anything you want.