Re: Class Definitions
On 11 Nov 2020 19:21:57 GMT r...@zedat.fu-berlin.de (Stefan Ram) wrote: > In my Python course I gave the assignment to define a > counter class "Main" so that > > counter0 = Main() > counter1 = Main() > counter1.count(); counter1.count(); counter1.count() > counter1.count(); counter1.count() > print( counter0.value ) > print( counter1.value ) > > would print > > 0 > 5 > > . > > I expected this solution: > > class Main: > def __init__( self ): > self.value = 0 > def count( self ): > self.value += 1 > > but a student turned in the following solution: > > class Main: > value = 0 > def count(self): > self.value += 1 > > . I am still a Python beginner and didn't even believe that the student's solution would work. I had expected an error as the instance variable self.value was not initialized. -- Manfred -- https://mail.python.org/mailman/listinfo/python-list
Re: Class Definitions
On 2020-11-14 at 10:09:32 +0100, Manfred Lotz wrote: > On 11 Nov 2020 19:21:57 GMT > r...@zedat.fu-berlin.de (Stefan Ram) wrote: > > > In my Python course I gave the assignment to define a > > counter class "Main" so that > > > > counter0 = Main() > > counter1 = Main() > > counter1.count(); counter1.count(); counter1.count() > > counter1.count(); counter1.count() > > print( counter0.value ) > > print( counter1.value ) > > > > would print > > > > 0 > > 5 > > > > . > > > > I expected this solution: > > > > class Main: > > def __init__( self ): > > self.value = 0 > > def count( self ): > > self.value += 1 > > > > but a student turned in the following solution: > > > > class Main: > > value = 0 > > def count(self): > > self.value += 1 > > > > . > > I am still a Python beginner and didn't even believe that the student's > solution would work. I had expected an error as the instance variable > self.value was not initialized. Remember: (1) x += 1 behaves like x = x + 1, and (2) bindings created inside a class statement but outside any method create class attributes. So after counter0 = Main(), Main (the class) has an attribute called "value" whose value is 0, and counter0.value refers to that attribute. Then counter0.count() executes self.value += 1, which behaves like self.value = self.value + 1. The right side of that assignment evaluates to 1 (the value of the class attribute plus the constant 1), and then the assignment statement initializes self.value to *that* value. There's nothing special about initializing instance attributes in __init__. An instance attribute can be created/initialized anywhere. HTH, Dan -- https://mail.python.org/mailman/listinfo/python-list
Re: Class Definitions
On Sat, 14 Nov 2020 05:08:07 -0600 2qdxy4rzwzuui...@potatochowder.com wrote: > On 2020-11-14 at 10:09:32 +0100, > Manfred Lotz wrote: > > > On 11 Nov 2020 19:21:57 GMT > > r...@zedat.fu-berlin.de (Stefan Ram) wrote: > > > > > In my Python course I gave the assignment to define a > > > counter class "Main" so that > > > > > > counter0 = Main() > > > counter1 = Main() > > > counter1.count(); counter1.count(); counter1.count() > > > counter1.count(); counter1.count() > > > print( counter0.value ) > > > print( counter1.value ) > > > > > > would print > > > > > > 0 > > > 5 > > > > > > . > > > > > > I expected this solution: > > > > > > class Main: > > > def __init__( self ): > > > self.value = 0 > > > def count( self ): > > > self.value += 1 > > > > > > but a student turned in the following solution: > > > > > > class Main: > > > value = 0 > > > def count(self): > > > self.value += 1 > > > > > > . > > > > I am still a Python beginner and didn't even believe that the > > student's solution would work. I had expected an error as the > > instance variable self.value was not initialized. > > Remember: (1) x += 1 behaves like x = x + 1, and (2) bindings created > inside a class statement but outside any method create class > attributes. > > So after counter0 = Main(), Main (the class) has an attribute called > "value" whose value is 0, and counter0.value refers to that attribute. > > Then counter0.count() executes self.value += 1, which behaves like > self.value = self.value + 1. The right side of that assignment > evaluates to 1 (the value of the class attribute plus the constant 1), > and then the assignment statement initializes self.value to *that* > value. > > There's nothing special about initializing instance attributes in > __init__. An instance attribute can be created/initialized anywhere. > > Thanks to you and Stefan. I did not know that if an instance variable doesn't exist the same name is searched for as a class variable. -- Manfred -- https://mail.python.org/mailman/listinfo/python-list
Re: Class Definitions
On 11/14/2020 4:09 AM, Manfred Lotz wrote: On 11 Nov 2020 19:21:57 GMT r...@zedat.fu-berlin.de (Stefan Ram) wrote: In my Python course I gave the assignment to define a counter class "Main" so that counter0 = Main() counter1 = Main() counter1.count(); counter1.count(); counter1.count() counter1.count(); counter1.count() print( counter0.value ) print( counter1.value ) would print 0 5 . I expected this solution: class Main: def __init__( self ): self.value = 0 def count( self ): self.value += 1 but a student turned in the following solution: class Main: value = 0 def count(self): self.value += 1 . I am still a Python beginner and didn't even believe that the student's solution would work. I had expected an error as the instance variable self.value was not initialized. It was not. But self.x as a value, rather than a target, first looks for instance x, then class x, then superclass x, up to object.x. Such lookup is routine for methods, much rarer for data. self.value += 1 is roughly equivalent to self.value = self.value +1 where the right self.value is Main.value. 'self' is only looked up once, but apparently the value attribute is looked up twice, in spite of what one might think from the doc, so that the first assignment to self.value initializes an instance value rather than overwriting the class value. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Debugging native cython module with visual studio toolchain
> > Hi, > > We developed a Python module that interfaces with native code via Cython. > > We currently build on Windows with Visual Studio Toolchain. > > We encounter the following issues when trying to build a debug version: > > 1) 3rd party modules installed via PIP are Release mode, but Visual Studio >> toolchain doesn't allow to mix Debug and Release libs. To workaround this >> issue, we build our module in "Release" mode, with debug symbols enabled, >> and with compiled optimization disabled (essentially a hack). > > 2) To build our module we currently use the following hack: > > step 1: run python.exe setup.py build --compiler=msvc > > step 2: extract the output > > step 3: change /Ox to /Od (disable compiler optimization) > > add /Zi flag to compiler flags (enable debug symbols) > > add /DEBUG flag to linker flags > > >> Please advise what is the best solution? > > -- https://mail.python.org/mailman/listinfo/python-list
Re: Debugging native cython module with visual studio toolchain
I can happily say I haven't used a Microsoft compiler/linker in decades, but: 1) Maybe clang-cl will do what you want? 2) Maybe it'd be easier to put debugging print's/printf's/cout <<'s in your code? HTH On Sat, Nov 14, 2020 at 1:55 PM Jeff wrote: > > > > Hi, > > > > We developed a Python module that interfaces with native code via Cython. > > > > We currently build on Windows with Visual Studio Toolchain. > > > > We encounter the following issues when trying to build a debug version: > > > > 1) 3rd party modules installed via PIP are Release mode, but Visual > Studio > >> toolchain doesn't allow to mix Debug and Release libs. To workaround > this > >> issue, we build our module in "Release" mode, with debug symbols > enabled, > >> and with compiled optimization disabled (essentially a hack). > > > > 2) To build our module we currently use the following hack: > > > > step 1: run python.exe setup.py build --compiler=msvc > > > > step 2: extract the output > > > > step 3: change /Ox to /Od (disable compiler optimization) > > > > add /Zi flag to compiler flags (enable debug symbols) > > > > add /DEBUG flag to linker flags > > > > > >> Please advise what is the best solution? > > > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list