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
> 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
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
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
> 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
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
- 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
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
> 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
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
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
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
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
13 matches
Mail list logo