Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20333 > unrolled thread

how to tell a method is classmethod or static method or instance method

Started byZheng Li <dllizheng@gmail.com>
First post2012-02-13 15:59 +0900
Last post2012-02-15 13:31 -0500
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  how to tell a method is classmethod or static method or instance method Zheng Li <dllizheng@gmail.com> - 2012-02-13 15:59 +0900
    Re: how to tell a method is classmethod or static method or instance method Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-13 08:03 +0000
      Re: how to tell a method is classmethod or static method or instance method 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-13 01:01 -0800
      Re: how to tell a method is classmethod or static method or instance method Nathan Rice <nathan.alexander.rice@gmail.com> - 2012-02-15 13:31 -0500

#20333 — how to tell a method is classmethod or static method or instance method

FromZheng Li <dllizheng@gmail.com>
Date2012-02-13 15:59 +0900
Subjecthow to tell a method is classmethod or static method or instance method
Message-ID<mailman.5754.1329116374.27778.python-list@python.org>
how to tell a method is class method or static method or instance method?

[toc] | [next] | [standalone]


#20336

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-02-13 08:03 +0000
Message-ID<4f38c3cc$0$11112$c3e8da3@news.astraweb.com>
In reply to#20333
On Mon, 13 Feb 2012 15:59:27 +0900, Zheng Li wrote:

> how to tell a method is class method or static method or instance
> method?

That's a good question, with a subtle answer that depends on exactly what 
you mean by the question. If you mean the object you get back from 
ordinary attribute access like "instance.method", then you do this:

>>> class K(object):
...     @classmethod
...     def cmethod(cls):
...             pass
...     @staticmethod
...     def smethod():
...             pass
...     def method(self):
...             pass
... 
>>> k = K()
>>> type(k.smethod)
<type 'function'>

So static methods are just functions, and both class methods and instance 
methods share the same underlying type:

>>> type(k.method)
<type 'instancemethod'>
>>> type(k.cmethod)
<type 'instancemethod'>


But if you dig deeper, you learn that all methods are actually 
descriptors:

>>> type(K.__dict__['cmethod'])
<type 'classmethod'>
>>> type(K.__dict__['smethod'])
<type 'staticmethod'>
>>> type(K.__dict__['method'])
<type 'function'>

(Functions are descriptors too.)

This is deep magic in Python, but if you want to learn more about it, you 
can read this:

http://users.rcn.com/python/download/Descriptor.htm


And I'll take this opportunity to plug my dualmethod descriptor:

http://code.activestate.com/recipes/577030-dualmethod-descriptor/



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#20339

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-02-13 01:01 -0800
Message-ID<5016707.681.1329123693532.JavaMail.geo-discussion-forums@pbsp9>
In reply to#20336
在 2012年2月13日星期一UTC+8下午4时03分24秒,Steven D&#39;Aprano写道:
> On Mon, 13 Feb 2012 15:59:27 +0900, Zheng Li wrote:
> 
> > how to tell a method is class method or static method or instance
> > method?
> 
> That's a good question, with a subtle answer that depends on exactly what 
> you mean by the question. If you mean the object you get back from 
> ordinary attribute access like "instance.method", then you do this:
> 
> >>> class K(object):
> ...     @classmethod
> ...     def cmethod(cls):
> ...             pass
> ...     @staticmethod
> ...     def smethod():
> ...             pass
> ...     def method(self):
> ...             pass
> ... 
> >>> k = K()
> >>> type(k.smethod)
> <type 'function'>
> 
> So static methods are just functions, and both class methods and instance 
> methods share the same underlying type:
> 
> >>> type(k.method)
> <type 'instancemethod'>
> >>> type(k.cmethod)
> <type 'instancemethod'>
> 
> 
> But if you dig deeper, you learn that all methods are actually 
> descriptors:
> 
> >>> type(K.__dict__['cmethod'])
> <type 'classmethod'>
> >>> type(K.__dict__['smethod'])
> <type 'staticmethod'>
> >>> type(K.__dict__['method'])
> <type 'function'>
> 
> (Functions are descriptors too.)
> 
> This is deep magic in Python, but if you want to learn more about it, you 
> can read this:
> 
> http://users.rcn.com/python/download/Descriptor.htm
> 
> 
> And I'll take this opportunity to plug my dualmethod descriptor:
> 
> http://code.activestate.com/recipes/577030-dualmethod-descriptor/
> 
> 
> 
> -- 
> Steven

The methods of an object can be well organized
to avoid redundant checks of operations 
desired to be performed on the object.

Also an object's methods should be factored to be easy to be maintained
 and to provide debugging and error information for the programmer to track
the operations related to the hardware, the OS, and the sofware design
issues. 



[toc] | [prev] | [next] | [standalone]


#20456

FromNathan Rice <nathan.alexander.rice@gmail.com>
Date2012-02-15 13:31 -0500
Message-ID<mailman.5840.1329330708.27778.python-list@python.org>
In reply to#20336
> And I'll take this opportunity to plug my dualmethod descriptor:
>
> http://code.activestate.com/recipes/577030-dualmethod-descriptor/

I use an analogous pattern in SQL Alchemy all the time (it's called
hybridmethod/hybridproperty there).

+1 to dualmethod, that pattern is great when you want a method or
property that does something concrete when passed an instance, or
something abstract relating to all instances when passed a class.


Nathan

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web