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 have "obj.foo(...)" as a si

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 expression. Consider: # P

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 sub-thre

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 find it now, so it must have

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. No there isn't. There is a

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 implicit binding of a variable

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 you'

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 construct

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 -- https://mail.python.org/m

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 terminology? Why Javascript and not >

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__’ leads people to wonder where

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 that ‘Foo.__init__’ is a co

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. > ... > * A constructor s

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 link you provided, it cl

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 people to wonder where the ‘self’ attribute > > cam

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 – am I not meant to be construc

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 the constructor, what do w

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 finish

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 instance of class [...]

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 pedan

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 under

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 fiel

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 order to instantiate twi

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 con

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 attributes. Class instances may over

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 the moment. > To cal

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 > th

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 return “false” if >

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* terminology

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

2016-12-12 Thread Thomas 'PointedEars' Lahn
burg, MS 39180', '8 Lake Forest Road > Princeton, NJ 08540') > > print(a.serial) # print: 100 > print(b.serial) # print: 101 > print(Box.serial) # print: 102 > > > The instructor said that the right way to call a class attribute is to use > 'Class.c

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

2016-12-11 Thread Steve D'Aprano
self.from_addr = from_addr > self.to_addr = to_addr > self.serial = Box.serial > Box.serial += 1 If you want to distinguish between an instance attribute and a class attribute, you must specify the instance or the class: self.serial # may be the instance attr

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

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 ('Cla

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

2016-12-11 Thread Chris Angelico
ld say that this is awkward usage; the class attribute isn't being used as a default for the instance, it's being used as "the next one". I would rename the class attribute to "next_serial". That would give you the freedom to use whichever notation you like, as there

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

2016-12-11 Thread Juan C.
ial) # print: 101 print(Box.serial) # print: 102 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 n

Re: class attribute

2016-01-28 Thread Joel Goldstick
On Thu, Jan 28, 2016 at 9:56 AM, Chris Angelico wrote: > On Fri, Jan 29, 2016 at 12:56 AM, jmp wrote: > > My guess is that models.Model has a metclass. Without going too much int > > details, the metaclass may change the class structure when it's created. > > > > django is very specific and very

Re: class attribute

2016-01-28 Thread Todd Dembrey
On 28/01/16 13:15, ast wrote: hello Here is a class from django framework from django.db import models class Article(models.Model): titre = models.CharField(max_length=100) auteur = models.CharField(max_length=42) contenu = models.TextField(null=True) date = models.DateTimeField(

Re: class attribute

2016-01-28 Thread Chris Angelico
On Fri, Jan 29, 2016 at 12:56 AM, jmp wrote: > My guess is that models.Model has a metclass. Without going too much int > details, the metaclass may change the class structure when it's created. > > django is very specific and very database oriented. > > " > article = Article(titre="Bonjour", aute

Re: class attribute

2016-01-28 Thread ast
"ast" a écrit dans le message de news:56aa1474$0$27833$426a7...@news.free.fr... OK, thank you for answer I didn't studied metaclass yet that's why it is not clear for me. I have to take a look at that concept first -- https://mail.python.org/mailman/listinfo/python-list

Re: class attribute

2016-01-28 Thread jmp
On 01/28/2016 02:15 PM, ast wrote: hello Here is a class from django framework from django.db import models class Article(models.Model): titre = models.CharField(max_length=100) auteur = models.CharField(max_length=42) contenu = models.TextField(null=True) date = models.DateT

class attribute

2016-01-28 Thread ast
hello Here is a class from django framework from django.db import models class Article(models.Model): titre = models.CharField(max_length=100) auteur = models.CharField(max_length=42) contenu = models.TextField(null=True) date = models.DateTimeField(auto_now_add=True, auto_now=Fal

Re: Accessing class attribute

2013-09-12 Thread Dave Angel
On 12/9/2013 02:15, chandan kumar wrote: > Hi , > > I'm new to python Welcome. I hope you enjoy your time here, and that the language treats you as well as it's treated me. > ,please correct me if there is any thing wrong with the way accessing class attributes. None of the following uses class

Re: Accessing class attribute

2013-09-12 Thread Peter Otten
Peter Otten wrote: > chandan kumar wrote: > >> Hi , >> >> I'm new to python ,please correct me if there is any thing wrong with the >> way accessing class attributes. >> >> Please see the below code .I have inherited confid in ExpectId class, >> changed self.print_msg to Hello. Now inherited

Re: Accessing class attribute

2013-09-11 Thread Peter Otten
chandan kumar wrote: > Hi , > > I'm new to python ,please correct me if there is any thing wrong with the > way accessing class attributes. > > Please see the below code .I have inherited confid in ExpectId class, > changed self.print_msg to Hello. Now inherited confid in TestprintmsgID > clas

Accessing class attribute

2013-09-11 Thread chandan kumar
Hi , I'm new to python ,please correct me if there is any thing wrong with the way accessing class attributes. Please see the below code .I have inherited confid in ExpectId class, changed self.print_msg to Hello. Now inherited confid in TestprintmsgID class.Now I wanted to print self.print

Re: Customizing class attribute access in classic classes

2011-10-30 Thread Geoff Bache
Thanks for this Steven. I'm however gettings some pretty odd effects, both on method access and inheritance. I expanded your example a bit... class Meta: def __init__(self, name, bases, namespace): self.__name__ = name self.__bases__ = bases self.__dict__ = namespace

Re: Customizing class attribute access in classic classes

2011-10-30 Thread Steven D'Aprano
On Sat, 29 Oct 2011 14:06:12 -0700, Geoff Bache wrote: > Hi, > > I'm wondering if there is any way to customize class attribute access on > classic classes? > > So this works: > > class Meta(type): > def __getattr__(cls, name): > retur

Re: Customizing class attribute access in classic classes

2011-10-30 Thread Geoff Bache
On Oct 30, 4:16 am, Ben Finney wrote: > Geoff Bache writes: > > I'm wondering if there is any way to customize class attribute access > > on classic classes? > > Why do that? What is it you're hoping to achieve, and why limit it to > classic classes on

Re: Customizing class attribute access in classic classes

2011-10-29 Thread Ben Finney
Geoff Bache writes: > I'm wondering if there is any way to customize class attribute access > on classic classes? Why do that? What is it you're hoping to achieve, and why limit it to classic classes only? > So this works: > > class Meta(type): >

Customizing class attribute access in classic classes

2011-10-29 Thread Geoff Bache
Hi, I'm wondering if there is any way to customize class attribute access on classic classes? So this works: class Meta(type): def __getattr__(cls, name): return "Customized " + name class A: __metaclass__ = Meta print A.blah but it turns A into a new-style c

Re: __set__ method is not called for class attribute access

2011-08-10 Thread Fuzzyman
On Aug 10, 5:27 pm, Eric Snow wrote: > On Wed, Aug 10, 2011 at 8:33 AM, Fuzzyman wrote: > > On Aug 5, 12:29 pm, Ryan wrote: > >> In the context of descriptors, the __set__ method is not called for > >> class attribute access. __set__ is only > >> called

Re: __set__ method is not called for class attribute access

2011-08-10 Thread Eric Snow
On Wed, Aug 10, 2011 at 8:33 AM, Fuzzyman wrote: > On Aug 5, 12:29 pm, Ryan wrote: >> In the context of descriptors, the __set__ method is not called for >> class attribute access. __set__ is only >> called to set the attribute on an instance instance of the owner class >

Re: __set__ method is not called for class attribute access

2011-08-10 Thread Duncan Booth
Fuzzyman wrote: >> The descriptor protocol only works when a value is being accessed or set >> on an instance and there is no instance attribute of that name so the >> value is fetched from the underlying class. >> > > That's not true. Properties, for example, can be got or set even when > they

Re: __set__ method is not called for class attribute access

2011-08-10 Thread Fuzzyman
On Aug 5, 1:16 pm, Duncan Booth wrote: > Ryan wrote: > > In the context of descriptors, the __set__ method is not called for > > class attribute access. __set__ is only > > called to set the attribute on an instance instance of the owner class > > to a new value, valu

Re: __set__ method is not called for class attribute access

2011-08-10 Thread Fuzzyman
On Aug 5, 12:29 pm, Ryan wrote: > In the context of descriptors, the __set__ method is not called for > class attribute access. __set__ is only > called to set the attribute on an instance instance of the owner class > to a new value, value. WHY? It's an unfortunate asymmetry

Re: __set__ method is not called for class attribute access

2011-08-07 Thread Duncan Booth
Peter Otten <__pete...@web.de> wrote: > Duncan Booth wrote: > >> The descriptor protocol only works when a value is being accessed or set >> on an instance and there is no instance attribute of that name so the >> value is fetched from the underlying class. > > Unlike normal class attributes a d

Re: __set__ method is not called for class attribute access

2011-08-05 Thread Peter Otten
Duncan Booth wrote: > The descriptor protocol only works when a value is being accessed or set > on an instance and there is no instance attribute of that name so the > value is fetched from the underlying class. Unlike normal class attributes a descriptor is not shaded by an instance attribute:

Re: __set__ method is not called for class attribute access

2011-08-05 Thread Duncan Booth
Ryan wrote: > In the context of descriptors, the __set__ method is not called for > class attribute access. __set__ is only > called to set the attribute on an instance instance of the owner class > to a new value, value. WHY? Is there some other mechanism for > accomplishing thi

Re: __set__ method is not called for class attribute access

2011-08-05 Thread Peter Otten
Ryan wrote: > In the context of descriptors, the __set__ method is not called for > class attribute access. __set__ is only > called to set the attribute on an instance instance of the owner class > to a new value, value. WHY? Is there some other mechanism for > accomplishing thi

__set__ method is not called for class attribute access

2011-08-05 Thread Ryan
In the context of descriptors, the __set__ method is not called for class attribute access. __set__ is only called to set the attribute on an instance instance of the owner class to a new value, value. WHY? Is there some other mechanism for accomplishing this outcome. This subtle difference from

Re: class attribute confusion

2010-12-05 Thread Steve Holden
On 12/3/2010 11:58 PM, Steven D'Aprano wrote: > Right. If you define a *class* attribute, it lives in the class, not the > instance, and so all instances share the same value. Unless, of course, an instance binds the same name in its namespace, in which case it will (usually) mas

Re: class attribute confusion

2010-12-04 Thread Steven D'Aprano
#x27;s not wrong at all. You expect "mylist" to behave as an instance >> attribute, but you defined it as a class attribute. Instance >> attributes are naturally initialised in the __init__() method. >> > Could you please point me to a reference in the doc?? http://

Re: class attribute confusion

2010-12-04 Thread Omar Abo-Namous
y)!! It's not wrong at all. You expect "mylist" to behave as an instance attribute, but you defined it as a class attribute. Instance attributes are naturally initialised in the __init__() method. Could you please point me to a reference in the doc?? Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list

Re: class attribute confusion

2010-12-03 Thread Steven D'Aprano
[1] > v: [1] > > Who wouldn't, right? Everybody who actually understands Python's object model. > The four variables v,x,y and z now actually share the same 'mylist'!! To > get the correct results, i have to initialize 'mylist' inside of the > __i

Re: class attribute confusion

2010-12-03 Thread Arnaud Delobelle
s behaviour is totally wrong, since it seems > A.__init__(self) is changing the value inside of A() not inside of the > object variable 'self' (that should be x or y)!! It's not wrong at all. You expect "mylist" to behave as an instance attribute, but you defined it as a class attribute. Instance attributes are naturally initialised in the __init__() method. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

class attribute confusion

2010-12-03 Thread OAN
Hi, i was having a problem with class attributes initiated outside of __init__. This code is a demonstration of what i mean: class A(): mylist = [] def __init__(self): self.mylist.append(1) pass class B(A): def __init__(self): A.__init__(self) self.

Re: how to cause a request for a missing class attribute cause its calculation

2010-05-19 Thread John Posner
On 5/18/2010 4:54 PM, Chris Rebert wrote: Suggested reading: http://docs.python.org/library/functions.html#property I've placed a revision to this official *property* documentation at: http://wiki.python.org/moin/AlternativeDescriptionOfProperty There's also a gentle (I hope) intro to t

Re: how to cause a request for a missing class attribute cause its calculation

2010-05-18 Thread Ethan Furman
Ethan Furman wrote: Vincent Davis wrote: Lets say I have class foo(object): def __init__(self, x, y): self.x=x self.y=y def xplusy(self): self.xy = x+y ^ this needs to be self.x + self.y inst = foo(1,2) inst.xy # no value, but I what this

Re: how to cause a request for a missing class attribute cause its calculation

2010-05-18 Thread Ethan Furman
Vincent Davis wrote: Lets say I have class foo(object): def __init__(self, x, y): self.x=x self.y=y def xplusy(self): self.xy = x+y ^ this needs to be self.x + self.y inst = foo(1,2) inst.xy # no value, but I what this to cause the calcul

Re: how to cause a request for a missing class attribute cause its calculation

2010-05-18 Thread Chris Rebert
On Tue, May 18, 2010 at 1:39 PM, Vincent Davis wrote: > > Lets say I have > class foo(object): >     def __init__(self, x, y): >         self.x=x >         self.y=y >     def xplusy(self): >         self.xy = x+y > inst = foo(1,2) > inst.xy   # no value, but I what this to cause the calculation of

how to cause a request for a missing class attribute cause its calculation

2010-05-18 Thread Vincent Davis
Lets say I have class foo(object): def __init__(self, x, y): self.x=x self.y=y def xplusy(self): self.xy = x+y inst = foo(1,2) inst.xy # no value, but I what this to cause the calculation of inst.xy I don't what to have self.xy calculated before it is called. T

Re: Access from a class attribute

2009-06-04 Thread Bruno Desthuilliers
Kless a écrit : Why can not to access from a class attribute to a function of that class? - class Foo(object): attr = __class__.__name__ attr = self.__class__.__name__ - "class" is an executable statement that instanciate a new class object a

Access from a class attribute

2009-06-04 Thread Kless
Why can not to access from a class attribute to a function of that class? - class Foo(object): attr = __class__.__name__ attr = self.__class__.__name__ - -- http://mail.python.org/mailman/listinfo/python-list

Re: Access from a class attribute

2009-06-04 Thread Ben Finney
Kless writes: > Why can not to access from a class attribute to a function of that > class? > > - > class Foo(object): >attr = __class__.__name__ >attr = self.__class__.__name__ > - The ‘self’ name is not magical. If you want it

Re: Using class-attribute as key to sort?

2008-08-29 Thread Terry Reedy
cnb wrote: In median grade, can I sort on Review-grade somehow? Google "Python sort key attribute" the first hit (after your post) is http://wiki.python.org/moin/HowTo/Sorting Find 'attribute' on that page for your answer (two options). -- http://mail.python.org/mailman/listinfo/python-list

Re: Using class-attribute as key to sort?

2008-08-29 Thread Chris Rebert
Just use the "key" argument to list.sort; e.g. class Foo(object): def __init__(self, x): self.x = x def __repr__(self): return "Foo:"+str(self.x) foos = [Foo(75), Foo(10), Foo(-1)] foos.sort(key = lambda foo: foo.x) print foos #=> [Foo:-1, Foo:10, Foo:75] - Chris

Re: Using class-attribute as key to sort?

2008-08-29 Thread bearophileHUGS
cnb: > In median grade, can I sort on Review-grade somehow? operator.attrgetter may help. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Using class-attribute as key to sort?

2008-08-29 Thread cnb
In median grade, can I sort on Review-grade somehow? something like: sr = self.reviews.sort(key=self.reviews.instance.grade) class Review(object): def __init__(self, movieId, grade, date): self.movieId = movieId self.grade = grade self.date = date class Customer(object

Re: Can't refer to base class attribute?

2007-10-23 Thread Marc 'BlackJack' Rintsch
On Tue, 23 Oct 2007 18:54:06 +, mrstephengross wrote: > I've got a Base class with an attribute "foo" (of type Foo), and a > Derived class (derived from Base). In Derived's constructor, I try to > refer to Base.foo, but python complains: > AttributeError: class Base has no attribute 'foo' B

Can't refer to base class attribute?

2007-10-23 Thread mrstephengross
I've got a Base class with an attribute "foo" (of type Foo), and a Derived class (derived from Base). In Derived's constructor, I try to refer to Base.foo, but python complains: AttributeError: class Base has no attribute 'foo' Any ideas? (code below) === CODE === #!/usr/bin/python class Foo:

Can anyone explain this behaviour (class attribute vs instance lookup)

2006-03-07 Thread Bruno da Silva de Oliveira
Hi all,From what I understand about the lookup semantics of instances and classes, the following is expected:>>> class A(object): pass...>>> A.__module__ '__main__'>>> A().__module__'__main__'IOW, if an attribute is not found in the instance, it is looked-up in class, and if found, returned. Then,

Re: Automatic class attribute

2006-02-03 Thread Franck PEREZ
;...: def showObservers(cls): > >...: print cls.observers > > > > class D1(C): > >...: observers = [] #could it be moved in C ? > > > > class D2(C): > >...: observers = [] #could it be moved in C ? > > > > I want eac

Re: Automatic class attribute

2006-02-03 Thread Kirk McDonald
...: observers = [] #could it be moved in C ? > > class D2(C): >...: observers = [] #could it be moved in C ? > > I want each child class of C to have it's own "observers" class attribute. > > The code I provided works... but I'd like to av

Automatic class attribute

2006-02-03 Thread Franck PEREZ
= [] #could it be moved in C ? I want each child class of C to have it's own "observers" class attribute. The code I provided works... but I'd like to avoid typing "observers = []" in each child class. Is it possible to define something in C which would basicall

Re: Implied instance attribute creation when referencing a class attribute

2006-01-16 Thread Russell Warren
Thanks for the additional examples, David (didn't see this before my last post). All of it makes sense now, including those examples. Russ -- http://mail.python.org/mailman/listinfo/python-list

Re: Implied instance attribute creation when referencing a class attribute

2006-01-16 Thread Russell Warren
D'oh... I just realized why this is happening. It is clear in the longhand as you say, but I don't think in the way you descibed it (or I'm so far gone right now I have lost it). self.I += 1 is the same as self.I = self.I + 1 and when python tries figures out what the 'self.I' is on the ri

Re: Implied instance attribute creation when referencing a class attribute

2006-01-16 Thread David Wahler
Russell Warren wrote: > Not true as above. The longhand for 'self.I += 1' is 'self.I = self.I > + 1', which normally needs self.I to exist due to the RHS of this. Try this: >>> class foo(object): ... I = 1 ... def __init__(self): ... print self.__dict__ ... self.I = self.I + 1 ...

Re: Implied instance attribute creation when referencing a class attribute

2006-01-16 Thread Russell Warren
' is not defined Note that x doesn't exists and it does not create it. You can't normally operate on something before it is created - Python won't create it for you (which is why I was surprised by the class attribute behavior in the first post). > If you write out the longh

Re: Implied instance attribute creation when referencing a class attribute

2006-01-16 Thread Chris Mellon
his I'm now > leaning more towards an AttributeError rather than allowing 'self.I' to > be synonymous with 'foo.I' if no local override). I can see how this can be confusing, but I think the confusion here is yours, not Pythons ;) self.I += 10 is an *assignment*. Like a

Implied instance attribute creation when referencing a class attribute

2006-01-16 Thread Russell Warren
I just ran across a case which seems like an odd exception to either what I understand as the "normal" variable lookup scheme in an instance/object heirarchy, or to the rules regarding variable usage before creation. Check this out: >>> class foo(object): ... I = 1 ... def __init__(self): ...

Re: puzzled about class attribute resolution and mangling

2005-12-11 Thread Brian van den Broek
James Stroud said unto the world upon 2005-12-09 20:39: > Brian van den Broek wrote: > >>Hi all, >> >>I've the following code snippet that puzzles me: >> >>class Base(object): >>__v, u = "Base v", "Base u" >>def __init__(self): >>print self.__v, self.u >> >>class Derived(Base): >>

Re: puzzled about class attribute resolution and mangling

2005-12-09 Thread James Stroud
Brian van den Broek wrote: > Hi all, > > I've the following code snippet that puzzles me: > > class Base(object): > __v, u = "Base v", "Base u" > def __init__(self): > print self.__v, self.u > > class Derived(Base): > __v, u = "Derived v", "Derived u" > def __init__(self)

puzzled about class attribute resolution and mangling

2005-12-09 Thread Brian van den Broek
Hi all, I've the following code snippet that puzzles me: class Base(object): __v, u = "Base v", "Base u" def __init__(self): print self.__v, self.u class Derived(Base): __v, u = "Derived v", "Derived u" def __init__(self): print self.__v, self.u sup

Re: class attribute to instance attribute

2005-07-05 Thread Greg Ewing
Donnal Walter wrote: > Each "presenter" (instance) needs its own "view" (instance). The class > attribute references a wxPython class. The resulting instance attribute > references a wxPython object (widget or container). This is a reasonable idea, but it would

Re: class attribute to instance attribute

2005-07-01 Thread Devan L
Well, I've never heard of a method like that for assigning variables. I'd rather put it in the __init__ method. -- http://mail.python.org/mailman/listinfo/python-list

Re: class attribute to instance attribute

2005-07-01 Thread Donnal Walter
Devan L wrote: > Why make it an instance attribute? Couldn't you just look at > the class attribute? Each "presenter" (instance) needs its own "view" (instance). The class attribute references a wxPython class. The resulting instance attribute references a wxPyt

Re: class attribute to instance attribute

2005-06-30 Thread Devan L
Why make it an instance attribute? Couldn't you just look at the class attribute? If its something that depends on each instance's value assigned to the attribute, why not make it an instance attribute to start with? -- http://mail.python.org/mailman/listinfo/python-list

class attribute to instance attribute

2005-06-30 Thread Donnal Walter
lse) >>> f = Frame() >>> isinstance(f.view, wx.Frame) True To summarize, each subclass has a class attribute "view" that is converted to an instance attribute of the same name at runtime. Is this a common Python idiom? If so, does it have a name? Is there a bett

Re: readonly class attribute ?

2005-03-15 Thread Bruno Desthuilliers
Bengt Richter a écrit : On Tue, 15 Mar 2005 20:21:19 +0100, bruno modulix <[EMAIL PROTECTED]> wrote: Hi How can I make a *class* attribute read-only ? (snip) Does this help, or did I misunderstand? >>> class Base(object): ... class __metaclass__(type): ... def __seta

Re: readonly class attribute ?

2005-03-15 Thread Bruno Desthuilliers
Simon Percivall a écrit : Start the attribute name with "_" and don't document it. If clients mess with it, they're to blame. The problem is that client code must *define* this attribute when subclassing BaseClass - and that's (well, in most case that should be) the only place where they have to

  1   2   >