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
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:
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
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:
> "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: '
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
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:
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
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
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
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
[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
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
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
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
"[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
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
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
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
"[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,
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
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
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
> > > 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
> > 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
>
> 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
> > 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(
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
> 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
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
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
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
>>
>>
>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.
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
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
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)
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
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?
> >
>
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
39 matches
Mail list logo