Re: Class Methods help

2009-06-01 Thread Jean-Michel Pichavant
FYI, same without decorators, if you python version does not support it. class MyClass: def some_func(x): return x+2 some_func = staticmethod(some_func) JM bd satish wrote: Thanks to Tim Chase & Lie Ryan !! That was exactly what I was looking for !! It's time for me to now rea

Re: Class Methods help

2009-05-31 Thread bd satish
Thanks to Tim Chase & Lie Ryan !! That was exactly what I was looking for !! It's time for me to now read the documentation of "decorators" and @classmethod and also @staticmethod. I'm quite new to decorators... -- Satish BD On Sun, May 31, 2009 at 4:44 PM, Lie Ryan wrote: > bdsatish wrote:

Re: Class Methods help

2009-05-31 Thread Lie Ryan
bdsatish wrote: > Hi, > > I have a question regarding the difference b/w "class methods" and > "object methods". Consider for example: > > class MyClass: > x = 10 > > Now I can access MyClass.x -- I want a similar thing for functions. I > tried > > class MyClass: > def some_func(x

Re: Class Methods help

2009-05-31 Thread Tim Chase
class MyClass: def some_func(x): return x+2 When I call MyClass.some_func(10) -- it fails, with error message: TypeError: unbound method some_func() must be called with MyClass instance as first argument (got int instance instead) OK. I figured out that something like this works:

Re: Class methods read-only by default?

2009-04-03 Thread Piet van Oostrum
> "Emanuele D'Arrigo" (ED) wrote: >ED> Hi Everybody! >ED> I just tried this: > class C(object): >ED> ...def method(self): >ED> ...pass >ED> ... > c = C() > delattr(c, "method") >ED> Traceback (most recent call last): >ED> File "", line 1, in >ED> AttributeError: '

Re: Class methods read-only by default?

2009-04-03 Thread Emanuele D'Arrigo
Thank you both, Steven and Andrew, for the insightful explanation. I shall keep it in mind when thinking about classes methods and instances. Thank you again. Manu -- http://mail.python.org/mailman/listinfo/python-list

Re: Class methods read-only by default?

2009-04-02 Thread Steven D'Aprano
On Thu, 02 Apr 2009 06:07:20 -0700, Emanuele D'Arrigo wrote: > Hi Everybody! > > I just tried this: > class C(object): > ...def method(self): > ...pass > ... c = C() delattr(c, "method") > > Traceback (most recent call last): > File "", line 1, in > AttributeError:

Re: Class methods read-only by default?

2009-04-02 Thread andrew cooke
Emanuele D'Arrigo wrote: > Hi Everybody! > > I just tried this: > class C(object): > ...def method(self): > ...pass > ... c = C() delattr(c, "method") > > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'C' object attribute 'method' is read-o

Re: Class Methods Vs Any Other Callable

2008-05-15 Thread [EMAIL PROTECTED]
On 15 mai, 17:53, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > Bruno Desthuilliers <[EMAIL PROTECTED]> writes: > > FWIW, I wonder why the BDFL choosed to implement __new__ as a > > staticmethod - there are probably some pretty good reasons, but not > > knowing them, it looks like __new__ would hav

Re: Class Methods Vs Any Other Callable

2008-05-15 Thread Arnaud Delobelle
Bruno Desthuilliers <[EMAIL PROTECTED]> writes: > FWIW, I wonder why the BDFL choosed to implement __new__ as a > staticmethod - there are probably some pretty good reasons, but not > knowing them, it looks like __new__ would have been a perfect > candidate for a classmethod. > > So far, the only

Re: Class Methods Vs Any Other Callable

2008-05-15 Thread Bruno Desthuilliers
Arnaud Delobelle a écrit : [EMAIL PROTECTED] wrote: On 14 mai, 22:44, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: On 14 mai, 19:45, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: __new__ is a static method! __new__ is a special-cased staticmethod

Re: Class Methods Vs Any Other Callable

2008-05-15 Thread Arnaud Delobelle
[EMAIL PROTECTED] wrote: > On 14 mai, 22:44, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > > "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > > > On 14 mai, 19:45, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > > >> __new__ is a static method! > > > > > __new__ is a special-cased staticmethod tha

Re: Class Methods Vs Any Other Callable

2008-05-15 Thread Bruno Desthuilliers
vbgunz a écrit : Instance methods make the most sense. A static method makes sense too *but* I can see how a class method not only does what a static method does but how a class method *also* gets the cls reference for free. I don't understand the last part - but I certainly agree on the "instan

Re: Class Methods Vs Any Other Callable

2008-05-15 Thread Bruno Desthuilliers
vbgunz a écrit : I remember learning closures in Python and thought it was the dumbest idea ever. Why use a closure when Python is fully object oriented? I didn't grasp the power/reason for them until I started learning JavaScript and then BAM, I understood them. Just a little while ago, I had a

Re: Class Methods Vs Any Other Callable

2008-05-15 Thread George Sakkis
On May 14, 4:38 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On 14 mai, 16:30, George Sakkis <[EMAIL PROTECTED]> wrote: > > > On May 14, 10:19 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > > > > > An instance method works on the instance > > > > A Static method is basically a function

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread Arnaud Delobelle
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > On 14 mai, 22:44, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: >> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: >> > On 14 mai, 19:45, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: >> >> __new__ is a static method! >> >> > __new__ is a special-cased

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread Ivan Illarionov
On Wed, 14 May 2008 09:21:10 -0700, vbgunz wrote: > [...] > when you see > one, what is the first thing that comes to mind? When you write one, > what was the first thing on your mind? Other than "similar to static- > methods", at what point will you be glad you used one? To sum it up, > what is t

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread [EMAIL PROTECTED]
On 14 mai, 22:44, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > > On 14 mai, 19:45, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > >> __new__ is a static method! > > > __new__ is a special-cased staticmethod that 1/ must not be declared > > as such

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread Carl Banks
On May 14, 12:21 pm, vbgunz <[EMAIL PROTECTED]> wrote: > Other than the 2 reasons above (2 making more sense), what is a really > good reason to pull out the class method. In other words, when you see > one, what is the first thing that comes to mind? When you write one, > what was the first thing

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread Arnaud Delobelle
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > On 14 mai, 19:45, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: >> __new__ is a static method! > > __new__ is a special-cased staticmethod that 1/ must not be declared > as such and 2/ takes the class object as first args. As far as I'm > concerned,

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread [EMAIL PROTECTED]
On 14 mai, 16:30, George Sakkis <[EMAIL PROTECTED]> wrote: > On May 14, 10:19 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > > > > An instance method works on the instance > > > A Static method is basically a function nested within a class object > > > A class method is overkill? > > > If anyt

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread [EMAIL PROTECTED]
On 14 mai, 19:45, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > George Sakkis <[EMAIL PROTECTED]> writes: > > On May 14, 10:19 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > > >> > An instance method works on the instance > >> > A Static method is basically a function nested within a class obj

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread Arnaud Delobelle
George Sakkis <[EMAIL PROTECTED]> writes: > On May 14, 10:19 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > >> > An instance method works on the instance >> > A Static method is basically a function nested within a class object >> > A class method is overkill? >> >> If anything, a static meth

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread vbgunz
> > > Instance methods make the most sense. A static method makes sense too > > > *but* I can see how a class method not only does what a static method > > > does but how a class method *also* gets the cls reference for free. > > > I don't understand the last part - but I certainly agree on the "in

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread vbgunz
> > Instance methods make the most sense. A static method makes sense too > > *but* I can see how a class method not only does what a static method > > does but how a class method *also* gets the cls reference for free. > > I don't understand the last part - but I certainly agree on the "instance >

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread Diez B. Roggisch
> When I learned about static methods, I learned they're a way to > tightly couple some functionality with a class without tying the > functionality to any of the instances. I see them as nothing more than > a design decision. To me they make some sense. Which you can say exactly about classmethod

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread vbgunz
> > An instance method works on the instance > > A Static method is basically a function nested within a class object > > A class method is overkill? > > If anything, a static method is overkill... > class Foo: > >   [EMAIL PROTECTED] >    def register(cls, listener): >        cls.LISTENERS.append(

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread George Sakkis
On May 14, 10:19 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > > An instance method works on the instance > > A Static method is basically a function nested within a class object > > A class method is overkill? > > If anything, a static method is overkill. See it this way: *if* you for some

Re: Class Methods Vs Any Other Callable

2008-05-14 Thread Diez B. Roggisch
> An instance method works on the instance > A Static method is basically a function nested within a class object > A class method is overkill? If anything, a static method is overkill. See it this way: *if* you for some reason put a method into an enclosing context - isn't it worth having a refer

Re: Class methods

2005-10-06 Thread Steve Holden
Gerrit Holl wrote: > Laszlo Zsolt Nagy wrote: > >>>Oh man, it has been a long time I have read such an disturbing question. >>> >>>RTMF here: http://docs.python.org/lib/built-in-funcs.html#l2h-14 >>> >>> >> >>I feel I was a bit harsh. > > > Of course, those posts do keep the Google count for t

Re: Class methods

2005-10-05 Thread Gerrit Holl
Laszlo Zsolt Nagy wrote: > >Oh man, it has been a long time I have read such an disturbing question. > > > >RTMF here: http://docs.python.org/lib/built-in-funcs.html#l2h-14 > > > > > I feel I was a bit harsh. Of course, those posts do keep the Google count for the famous four-letter-abbreviatio

Re: Class methods

2005-10-05 Thread Fredrik Lundh
Class methods"Hughes, Chad O" wrote: > Is there any way to create a class method? somewhat ironically, the first google hit for that sentence is a chapter in the python tutorial that *doesn't* explain how to create class methods... as others have pointed out, the classmethod wrapper/descriptor i

Re: Class methods

2005-10-05 Thread Laszlo Zsolt Nagy
>> >> >Oh man, it has been a long time I have read such an disturbing question. > >RTMF here: http://docs.python.org/lib/built-in-funcs.html#l2h-14 > > I feel I was a bit harsh. class A(object): x = 0 @classmethod def f(cls): cls.x += 1 print "x is",cls.x >>> A.

Re: Class methods

2005-10-05 Thread Laszlo Zsolt Nagy
Hughes, Chad O wrote: > Is there any way to create a class method? I can create a class > variable like this: > ... > Any ideas? > Oh man, it has been a long time I have read such an disturbing question. RTMF here: http://docs.python.org/lib/built-in-funcs.html#l2h-14 Les -- http://mail

Re: Class methods in Python/C?

2004-11-30 Thread Craig Ringer
On Wed, 2004-12-01 at 00:15, Thomas Heller wrote: > To answer the original question: To create class methods in C code, you > use the METH_CLASS flag in the PyMethodDef structure. Supported in > Python 2.3 and above, in 2.2 it is more complicated. If needed, I can > post a snippet for 2.2 as wel

Re: Class methods in Python/C? [ANSWER]

2004-11-30 Thread Craig Ringer
On Tue, 2004-11-30 at 20:39, Nick Coghlan wrote: > You probably want to look at staticmethod(). (classmethod() is slightly > different, and probably not what you want. In fact, classmethod() is > practically > *never* what you want. Guido wrote it himself, and even he ended up not using > it)

Re: Class methods in Python/C?

2004-11-30 Thread Thomas Heller
Jp Calderone <[EMAIL PROTECTED]> writes: > On Tue, 30 Nov 2004 22:39:15 +1000, Nick Coghlan <[EMAIL PROTECTED]> wrote: >>Craig Ringer wrote: >> > Hi folks >> > >> > I've been doing some looking around, but have been unable to find out >> > how to implement class methods on Python objects written

Re: Class methods in Python/C?

2004-11-30 Thread Jp Calderone
On Tue, 30 Nov 2004 22:39:15 +1000, Nick Coghlan <[EMAIL PROTECTED]> wrote: >Craig Ringer wrote: > > Hi folks > > > > I've been doing some looking around, but have been unable to find out > > how to implement class methods on Python objects written in C. "Why are > > you using C?" you ask? > > >

Re: Class methods in Python/C?

2004-11-30 Thread Nick Coghlan
Craig Ringer wrote: Hi folks I've been doing some looking around, but have been unable to find out how to implement class methods on Python objects written in C. "Why are you using C?" you ask? Yeah, so do I. However, I need to provide bindings for an application that Python is embedded into, and t