Re: Instance vs Class variable oddity

2019-05-18 Thread jfong
Chris Angelico於 2019年5月18日星期六 UTC+8下午3時09分37秒寫道: > On Sat, May 18, 2019 at 1:51 PM wrote: > > > > Correct me if I am wrong, please. > > > > I always think that the LEGB rule (e.g. the namespace to look up for) was > > applied at compile-time, only the binding was resolved "dynamically" at > > ru

Re: Instance vs Class variable oddity

2019-05-18 Thread Chris Angelico
On Sat, May 18, 2019 at 1:51 PM wrote: > > Correct me if I am wrong, please. > > I always think that the LEGB rule (e.g. the namespace to look up for) was > applied at compile-time, only the binding was resolved "dynamically" at > run-time. For example: > > def foo(): > print(x) > > foo() wi

Re: Instance vs Class variable oddity

2019-05-17 Thread dieter
Irv Kalb writes: > ... > The only thing that threw me was that in a line like: > > self.x = self.x + 1 > > in a method, these two uses of self.x can refer to different variables. I > actually teach Python, and this would be a very difficult thing to explain to > students. > > I have never run

Re: Instance vs Class variable oddity

2019-05-17 Thread jfong
Correct me if I am wrong, please. I always think that the LEGB rule (e.g. the namespace to look up for) was applied at compile-time, only the binding was resolved "dynamically" at run-time. For example: def foo(): print(x) foo() will cause a NameError. But after x = 5 foo() will run corr

Re: Instance vs Class variable oddity

2019-05-17 Thread Ethan Furman
On 05/17/2019 11:37 AM, Irv Kalb wrote: self.x = self.x + 1 I have never run across this issue because I would never use the same name as an instance attribute and a class attribute. So you treat your class attributes as if they were static? -- ~Ethan~ -- https://mail.python.org/mailman/lis

Re: Instance vs Class variable oddity

2019-05-17 Thread Chris Angelico
On Sat, May 18, 2019 at 4:40 AM Irv Kalb wrote: > > Thanks for your comments. I am very aware of all the other issues that you > explained. > > The only thing that threw me was that in a line like: > > self.x = self.x + 1 > > in a method, these two uses of self.x can refer to different variables

Re: Instance vs Class variable oddity

2019-05-17 Thread Irv Kalb
> On May 15, 2019, at 5:41 PM, Ben Finney wrote: > > Irv Kalb writes: > >> I just saw some code that confused me. The confusion has to do with >> class variables and instance variables. > > (Perhaps unrelated, but here's another confusion you may be suffering > from: There's no such thing as

Re: Instance vs Class variable oddity

2019-05-15 Thread Ben Finney
Irv Kalb writes: > I just saw some code that confused me. The confusion has to do with > class variables and instance variables. (Perhaps unrelated, but here's another confusion you may be suffering from: There's no such thing as a “class variable” or “instance variable”. In Python, a “variable

Re: Instance vs Class variable oddity

2019-05-15 Thread Irv Kalb
> On May 15, 2019, at 11:02 AM, Rob Gaddi > wrote: > > On 5/15/19 10:52 AM, Irv Kalb wrote: >> I just saw some code that confused me. The confusion has to do with class >> variables and instance variables. In order to understand it better, I built >> this very small example: >> class Test:

Re: Instance vs Class variable oddity

2019-05-15 Thread Rob Gaddi
On 5/15/19 10:52 AM, Irv Kalb wrote: I just saw some code that confused me. The confusion has to do with class variables and instance variables. In order to understand it better, I built this very small example: class Test: x = 0 def __init__(self, id): self.id = id

Instance vs Class variable oddity

2019-05-15 Thread Irv Kalb
I just saw some code that confused me. The confusion has to do with class variables and instance variables. In order to understand it better, I built this very small example: class Test: x = 0 def __init__(self, id): self.id = id self.show() def show(self):