Re: The right way to 'call' a class attribute inside the same class

2016-12-19 Thread Thomas 'PointedEars' Lahn
Chris Angelico wrote: > On Mon, Dec 19, 2016 at 9:52 AM, Erik wrote: >> 1) Method call: >>"obj.foo(1, 2, 3)" is syntactic sugar for "obj.foo(obj, 1, 2, 3)". > > And the bit you have to be REALLY careful of when working with both > Python and JS is that you have to

Re: The right way to 'call' a class attribute inside the same class

2016-12-18 Thread Chris Angelico
On Mon, Dec 19, 2016 at 9:52 AM, Erik wrote: > > 1) Method call: >"obj.foo(1, 2, 3)" is syntactic sugar for "obj.foo(obj, 1, 2, 3)". And the bit you have to be REALLY careful of when working with both Python and JS is that you have to have "obj.foo(...)" as a single

Re: The right way to 'call' a class attribute inside the same class

2016-12-18 Thread Erik
NOTE: If you found this message by searching for help on how Python works, be aware that it's discussing how JavaScript works, not Python! Look elsewhere :) Chris, this isn't directed at you (I think you get it) - just following up with some detail for anyone who might discover this

Re: The right way to 'call' a class attribute inside the same class

2016-12-17 Thread Chris Angelico
On Sun, Dec 18, 2016 at 12:01 PM, Erik wrote: > Yes, in that case there is (I didn't grok that you meant using 'new' by > "calling a function as a constructor", but it's obvious now you spell it > out). Yeah. I thought that I got that terminology from MDN, but I can't

Re: The right way to 'call' a class attribute inside the same class

2016-12-17 Thread Erik
On 16/12/16 01:17, Chris Angelico wrote: On Fri, Dec 16, 2016 at 11:36 AM, Erik wrote: On 12/12/16 23:23, Chris Angelico wrote: In JavaScript, it's normal to talk about "calling a function as a constructor". When you do, there is a 'this' object before you start.

Re: The right way to 'call' a class attribute inside the same class

2016-12-15 Thread Chris Angelico
On Fri, Dec 16, 2016 at 11:36 AM, Erik wrote: > On 12/12/16 23:23, Chris Angelico wrote: >> >> In JavaScript, it's normal to talk about "calling a function as a >> constructor". When you do, there is a 'this' object before you start. > > > No there isn't. There is an

Re: The right way to 'call' a class attribute inside the same class

2016-12-15 Thread Erik
On 12/12/16 23:23, Chris Angelico wrote: In JavaScript, it's normal to talk about "calling a function as a constructor". When you do, there is a 'this' object before you start. No there isn't. There is an implicit binding of a variable called "this" based on the syntactic sugar of whether

Re: The right way to 'call' a class attribute inside the same class

2016-12-15 Thread Erik
On 13/12/16 06:14, Gregory Ewing wrote: Ned Batchelder wrote: if a C++ constructor raises an exception, will the corresponding destructor be run, or not? (No, because it never finished making an object of type T.) So it just leaks any memory that's been allocated by the partially-run

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Gregory Ewing
Ned Batchelder wrote: if a C++ constructor raises an exception, will the corresponding destructor be run, or not? (No, because it never finished making an object of type T.) So it just leaks any memory that's been allocated by the partially-run constructor? -- Greg --

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Chris Angelico
On Tue, Dec 13, 2016 at 4:45 PM, Steven D'Aprano wrote: > I don't understand the point of bringing up Javascript. Ben has already said > that we shouldn't feel the need to mindlessly copy C++ terminology. Is it your > position that we *should* copy Javascript

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Steven D'Aprano
On Tuesday 13 December 2016 10:23, Chris Angelico wrote: > On Tue, Dec 13, 2016 at 10:17 AM, Ben Finney > wrote: >> If the differences didn't matter I would agree that “overly pedantic” is >> fair. But those differences trip up newcomers. Thinking of >> ‘Foo.__init__’

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Steven D'Aprano
On Tuesday 13 December 2016 12:12, Ned Batchelder wrote: > On Monday, December 12, 2016 at 6:17:43 PM UTC-5, Ben Finney wrote: >> Ned Batchelder writes: >> >> > Claiming that __init__ isn't a constructor seems overly pedantic to >> > me. >> >> Whereas to me, claiming

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Ned Batchelder
On Monday, December 12, 2016 at 6:17:43 PM UTC-5, Ben Finney wrote: > Ned Batchelder writes: > > > Claiming that __init__ isn't a constructor seems overly pedantic to > > me. > > Whereas to me, claiming that ‘Foo.__init__’ is a constructor seems > needlessly confusing. >

Re: Python constructors have particular semantics, and ‘Foo.__init__’ doesn't qualify (was: The right way to 'call' a class attribute inside the same class)

2016-12-12 Thread Juan C.
I agree with you, I'll post here the same thing I said in there for another member: On Mon, Dec 12, 2016 at 6:59 PM, Thomas 'PointedEars' Lahn wrote: > > Using the Python official doc

Python constructors have particular semantics, and ‘Foo.__init__’ doesn't qualify (was: The right way to 'call' a class attribute inside the same class)

2016-12-12 Thread Ben Finney
Chris Angelico writes: > On Tue, Dec 13, 2016 at 10:17 AM, Ben Finney > wrote: > > If the differences didn't matter I would agree that “overly > > pedantic” is fair. But those differences trip up newcomers. Thinking > > of ‘Foo.__init__’ leads

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Chris Angelico
On Tue, Dec 13, 2016 at 10:17 AM, Ben Finney wrote: > If the differences didn't matter I would agree that “overly pedantic” is > fair. But those differences trip up newcomers. Thinking of > ‘Foo.__init__’ leads people to wonder where the ‘self’ attribute came > from –

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Ben Finney
Ned Batchelder writes: > Claiming that __init__ isn't a constructor seems overly pedantic to > me. Whereas to me, claiming that ‘Foo.__init__’ is a constructor seems needlessly confusing. * Classes already have a constructor, ‘Foo.__new__’. If we call something else

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Steve D'Aprano
On Tue, 13 Dec 2016 07:15 am, Ned Batchelder wrote: > Claiming that __init__ isn't a constructor seems overly pedantic to me. > What's true is that Python's constructors (__init__) are different than > C++ constructors. In C++, you don't have an object of type T until the > constructor has

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Juan C.
On Mon, Dec 12, 2016 at 6:59 PM, Thomas 'PointedEars' Lahn wrote: > Using the Python official doc link you provided, it clearly states that `__new__` is the one called to "create a new

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Ned Batchelder
On Monday, December 12, 2016 at 4:31:00 PM UTC-5, Gregory Ewing wrote: > Ned Batchelder wrote: > > In C++, you don't have an object of type T until the > > constructor has finished. In Python, you have an object of type T before > > __init__ has been entered. > > That distinction seems a bit

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Steve D'Aprano
On Tue, 13 Dec 2016 03:17 am, Chris Angelico wrote: > You could check "foo" in self.__dict__, but I don't know of any > real-world situations where you need to. vars(self) is probably the better way to access self's namespace, rather than directly self.__dict__. Unfortunately vars() doesn't

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Gregory Ewing
Ned Batchelder wrote: In C++, you don't have an object of type T until the constructor has finished. In Python, you have an object of type T before __init__ has been entered. That distinction seems a bit pedantic as well. Inside a C++ constructor you have access to something having all the

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Thomas 'PointedEars' Lahn
Juan C. wrote: > On Mon, Dec 12, 2016 at 12:34 PM, Thomas 'PointedEars' Lahn > wrote: >> To call something means generally in programming, and in Python, to >> execute it as a function instead: In the code above, the class object >> referred to by “Box” is called twice in

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Ned Batchelder
On Monday, December 12, 2016 at 12:58:30 PM UTC-5, Juan C. wrote: > Since we are talking about Python terminology I believe that calling > `__init__` a constructor is also wrong. I've already seem some > discussions regarding it and the general consensus is that `__init__` > shouldn't be called

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread John Gordon
In "Juan C." writes: > The instructor said that the right way to call a class attribute is to use > 'Class.class_attr' notation, but on the web I found examples where people > used 'self.class_attr' to call class

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Juan C.
On Mon, Dec 12, 2016 at 12:34 PM, Thomas 'PointedEars' Lahn wrote: > First of all, the proper term for what you are doing there is _not_ “call”; > you are _accessing_ an attribute instead. Indeed, `accessing` seems better. I was looking for a better word but couldn't find at

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Juan C.
On Sun, Dec 11, 2016 at 11:34 PM, Steve D'Aprano wrote: > So... in summary: > > > When *assigning* to an attribute: > > - use `self.attribute = ...` when you want an instance attribute; > > - use `Class.attribute = ...` when you want a class attribute in > the same

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Chris Angelico
On Tue, Dec 13, 2016 at 1:34 AM, Thomas 'PointedEars' Lahn wrote: > ³ How can one tell the difference in Python between a pre-initialized, >inherited attribute value and one own that is just equal to the inherited >one? In ECMAScript, this.hasOwnProperty("foo") would

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Thomas 'PointedEars' Lahn
Thomas 'PointedEars' Lahn wrote: > Note that (AIUI) in this example the instances of the class referred by > “C” do not have an *own* “foo” property in the beginning, so until bar() > is called on them, they inherit that property (and its value) from that > class.³ For proper *Python*

Re: The right way to 'call' a class attribute inside the same class

2016-12-12 Thread Thomas 'PointedEars' Lahn
Juan C. wrote: > I'm watching a Python course and was presented a topic regarding classes. > One of the examples were: > > box.py > > class Box: > serial = 100 > > def __init__(self, from_addr, to_addr): > self.from_addr = from_addr > self.to_addr = to_addr >

Re: The right way to 'call' a class attribute inside the same class

2016-12-11 Thread Steve D'Aprano
On Mon, 12 Dec 2016 07:10 am, Juan C. wrote: > I'm watching a Python course and was presented a topic regarding classes. > One of the examples were: > > box.py > > class Box: > serial = 100 > > def __init__(self, from_addr, to_addr): > self.from_addr = from_addr >

Re: The right way to 'call' a class attribute inside the same class

2016-12-11 Thread Terry Reedy
On 12/11/2016 5:29 PM, Gregory Ewing wrote: Juan C. wrote: The instructor said that the right way to call a class attribute is to use 'Class.class_attr' notation, but on the web I found examples where people used 'self.class_attr' to call class attributes. I believe that using the first

Re: The right way to 'call' a class attribute inside the same class

2016-12-11 Thread Gregory Ewing
Juan C. wrote: The instructor said that the right way to call a class attribute is to use 'Class.class_attr' notation, but on the web I found examples where people used 'self.class_attr' to call class attributes. I believe that using the first notation is better ('Class.class_attr'), this way

Re: The right way to 'call' a class attribute inside the same class

2016-12-11 Thread Chris Angelico
On Mon, Dec 12, 2016 at 7:10 AM, Juan C. wrote: > class Box: > serial = 100 > > def __init__(self, from_addr, to_addr): > self.from_addr = from_addr > self.to_addr = to_addr > self.serial = Box.serial > Box.serial += 1 > I would

The right way to 'call' a class attribute inside the same class

2016-12-11 Thread Juan C.
I'm watching a Python course and was presented a topic regarding classes. One of the examples were: box.py class Box: serial = 100 def __init__(self, from_addr, to_addr): self.from_addr = from_addr self.to_addr = to_addr self.serial = Box.serial