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


Groups > comp.lang.python > #60285

Re: Behavior of staticmethod in Python 3

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news.space.net!news.osn.de!diablo2.news.osn.de!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'attribute': 0.07; 'python3': 0.07; 'executed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; '(none,': 0.16; '*args):': 0.16; 'callable': 0.16; 'foo():': 0.16; 'marco': 0.16; 'metaclass': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'typeerror:': 0.16; 'who?': 0.16; 'all.': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'body,': 0.24; 'skip': 0.24; 'script': 0.25; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; "doesn't": 0.30; 'skip:@ 10': 0.30; '"",': 0.31; 'directly,': 0.31; 'object.': 0.31; 'file': 0.32; 'class': 0.32; 'says': 0.33; '(most': 0.33; 'skip:_ 10': 0.34; 'classes': 0.35; 'but': 0.35; 'false': 0.36; 'should': 0.36; 'two': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'details.': 0.61; 'not:': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Behavior of staticmethod in Python 3
Date Sat, 23 Nov 2013 10:01:45 +0100
Organization None
References <l6povp$ndp$1@speranza.aioe.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084a963.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3076.1385197282.18130.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385197282 news.xs4all.nl 15982 [2001:888:2000:d::a6]:58206
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60285

Show key headers only | View raw


Marco Buttu wrote:

> In Python 3 the following two classes should be equivalent:

Says who?

> $ cat foo.py
> class Foo:
>      def foo():
>          pass
>      print(callable(foo))
> 
> class Foo:
>      @staticmethod
>      def foo():
>          pass
>      print(callable(foo))
> 
> But they do not:
> 
> $ python3 foo.py
> True
> False
> 
> How come the metaclass does not skip the staticmethod decorator?

What? Your print()s are executed inside the class body, so the classes don't 
come into play at all.

Your script is saying that a staticmethod instance is not a callable object. 
It need not be because

Foo.foo()

doesn't call the Foo.foo attribute directly, it calls

Foo.foo.__get__(None, Foo)()

>>> class D:
...     def __get__(self, *args): print(args)
... 
>>> class Foo:
...     foo = D()
... 
>>> Foo.foo()
(None, <class '__main__.Foo'>)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

Look for "descriptor protocol" to learn the details.

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


Thread

Behavior of staticmethod in Python 3 Marco Buttu <marco.buttu@gmail.com> - 2013-11-23 09:28 +0100
  Re: Behavior of staticmethod in Python 3 Peter Otten <__peter__@web.de> - 2013-11-23 10:01 +0100
    Re: Behavior of staticmethod in Python 3 Marco Buttu <marco.buttu@gmail.com> - 2013-11-23 10:39 +0100
      Re: Behavior of staticmethod in Python 3 Peter Otten <__peter__@web.de> - 2013-11-23 16:23 +0100
    Re: Behavior of staticmethod in Python 3 Marco Buttu <marco.buttu@gmail.com> - 2013-11-23 10:39 +0100
  Re: Behavior of staticmethod in Python 3 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-23 13:48 +0000
  Re: Behavior of staticmethod in Python 3 Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-23 16:00 +0100
  Re: Behavior of staticmethod in Python 3 Chris Angelico <rosuav@gmail.com> - 2013-11-24 08:38 +1100
  Re: Behavior of staticmethod in Python 3 Peter Otten <__peter__@web.de> - 2013-11-23 22:51 +0100
  Re: Behavior of staticmethod in Python 3 Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-24 11:30 +0100
    Re: Behavior of staticmethod in Python 3 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-24 14:18 +0000
  Re: Behavior of staticmethod in Python 3 Peter Otten <__peter__@web.de> - 2013-11-24 11:43 +0100
  Re: Behavior of staticmethod in Python 3 Ian Kelly <ian.g.kelly@gmail.com> - 2013-11-24 03:45 -0700
  Re: Behavior of staticmethod in Python 3 Peter Otten <__peter__@web.de> - 2013-11-24 12:03 +0100
  Re: Behavior of staticmethod in Python 3 Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-24 16:55 +0100
  Re: Behavior of staticmethod in Python 3 Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-24 17:08 +0100

csiph-web