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


Groups > comp.lang.python > #20339

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

From 88888 Dihedral <dihedral88888@googlemail.com>
Newsgroups comp.lang.python
Subject Re: how to tell a method is classmethod or static method or instance method
Date 2012-02-13 01:01 -0800
Organization http://groups.google.com
Message-ID <5016707.681.1329123693532.JavaMail.geo-discussion-forums@pbsp9> (permalink)
References <mailman.5754.1329116374.27778.python-list@python.org> <4f38c3cc$0$11112$c3e8da3@news.astraweb.com>

Show all headers | View raw


在 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. 



Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web