Re: [Tutor] Class vs. Static Methods

2005-06-23 Thread Kent Johnson
Alan G wrote: >>This is a neat trick. But can't this also be done with a static > > method > >>that accesses a static data attribute the same way? > > > I don't think so because the static mehod can only see > the attributes of Shape not of Line. Well, it can *see* Point._count and Line._coun

Re: [Tutor] Class vs. Static Methods

2005-06-23 Thread Alan G
> This is a neat trick. But can't this also be done with a static method > that accesses a static data attribute the same way? I don't think so because the static mehod can only see the attributes of Shape not of Line. It doesn't have access to the cls value in Kent's code below... > >> @classm

Re: [Tutor] Class vs. Static Methods

2005-06-22 Thread Chuck Allison
Hello Kent, This is the killer example I've been looking for. Now I understand. Sorry I've been so dense. This is way cool. Thanks. Wednesday, June 22, 2005, 4:39:38 AM, you wrote: KJ> Not sure why you think you have to write a new classmethod for KJ> each shape. Suppose you want to maintain cre

Re: [Tutor] Class vs. Static Methods

2005-06-22 Thread Chuck Allison
This is a neat trick. But can't this also be done with a static method that accesses a static data attribute the same way? Alan G wrote: >>class Shape(object): >> _count = 0 >> >> @classmethod >> def count(cls): >>try: >> cls._count += 1 >>except AttributeError: >> cls

Re: [Tutor] Class vs. Static Methods

2005-06-22 Thread Alan G
> class Shape(object): > _count = 0 > > @classmethod > def count(cls): > try: > cls._count += 1 > except AttributeError: > cls._count = 1 Ah, clever. This is where I thought I'd need an if/elif chain, adding a new clause for each subclass. i never thought of usin

Re: [Tutor] Class vs. Static Methods

2005-06-22 Thread Kent Johnson
Alan G wrote: > So If I have a heirarchy of shapes and want a class method that > only operates on the shape class itself, not on all the > subclasses then I have to use staticmethod whereas if I want > the class method to act on shape and each of its sub classes > I must use a classmethod. The can

Re: [Tutor] Class vs. Static Methods

2005-06-22 Thread Alan G
- Original Message - From: "Kent Johnson" <[EMAIL PROTECTED]> > No, a classmethod is passed the class that it is called on. > If you have an inheritance tree you don't know this with a staticmethod. Aha! Like the OP I was aware of the class/no class distinction but couldn't see how this

Re: [Tutor] Class vs. Static Methods

2005-06-21 Thread Kent Johnson
Chuck Allison wrote: > Hello Chinook, > > So is the main motivation for class methods so that you can have the > class object available? It seems you can have that anyway in a static > method by just asking. No, a classmethod is passed the class that it is called on. If you have an inheritance t

Re: [Tutor] Class vs. Static Methods

2005-06-21 Thread Alan G
> Sorry for the elementary question: I was wondering if someone could > explain the difference to me between class and static methods. Coming > from other languages, I'm used to static methods, but not "class > methods". Thanks. There probably is a deep and subtle difference in Python but to all

Re: [Tutor] Class vs. Static Methods

2005-06-21 Thread Chinook
On Tue, 21 Jun 2005 17:58:09 -0400, Chuck Allison wrote (in article <[EMAIL PROTECTED]>): > Hello Chinook, > > So is the main motivation for class methods so that you can have the > class object available? It seems you can have that anyway in a static > method by just asking. I'm sure there's a g

Re: [Tutor] Class vs. Static Methods

2005-06-21 Thread Chuck Allison
Hello Chinook, So is the main motivation for class methods so that you can have the class object available? It seems you can have that anyway in a static method by just asking. I'm sure there's a good reason for this, but I haven't yet gotten to the point of mastery where I can see a need for clas

Re: [Tutor] Class vs. Static Methods

2005-06-21 Thread Chinook
On Tue, 21 Jun 2005 16:52:09 -0400, Chuck Allison wrote (in article <[EMAIL PROTECTED]>): > Sorry for the elementary question: I was wondering if someone could > explain the difference to me between class and static methods. Coming > from other languages, I'm used to static methods, but not "class

[Tutor] Class vs. Static Methods

2005-06-21 Thread Chuck Allison
Sorry for the elementary question: I was wondering if someone could explain the difference to me between class and static methods. Coming from other languages, I'm used to static methods, but not "class methods". Thanks. -- Best regards, Chuck ___ Tut