Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!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: 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; 'argument': 0.05; 'subject:Python': 0.06; 'attribute': 0.07; 'python3': 0.07; '-10': 0.09; 'attributes': 0.09; 'function:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:% 20': 0.09; 'solution,': 0.09; 'wrong,': 0.09; 'python': 0.11; 'def': 0.12; "%s'": 0.16; 'callable': 0.16; 'foo():': 0.16; 'instance:': 0.16; 'instead).': 0.16; 'marco': 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; 'value))': 0.16; 'valueerror': 0.16; 'who?': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'import': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'config': 0.24; 'non': 0.24; 'script': 0.25; 'class.': 0.26; 'define': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'am,': 0.29; 'raise': 0.29; 'skip:@ 10': 0.30; 'object.': 0.31; 'raised': 0.31; 'class': 0.32; 'skip:c 30': 0.32; 'says': 0.33; '(most': 0.33; 'skip:_ 10': 0.34; 'classes': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'method': 0.36; 'should': 0.36; 'positive': 0.37; '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; 'name:': 0.61; 'first': 0.61; 'name': 0.63; 'body.': 0.68; 'otten': 0.84; '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 16:23:53 +0100 Organization: None References: <529077E0.8090603@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084ac13.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 93 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385220205 news.xs4all.nl 15874 [2001:888:2000:d::a6]:42599 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60306 Marco Buttu wrote: > On 11/23/2013 10:01 AM, Peter Otten 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 >>> >> >> Your script is saying that a staticmethod instance is not a callable >> object. It need not be because >> >> Foo.foo() > > Yes, you are right about Python 3. But in Python 2, if I am not going > wrong, there is not solution, and I need to define a function outside > the class. For instance: > > $ cat config.py > class Configuration(object): > > def positiveCheck(value): > if not value > 0: > raise AttributeError('Must be a positive number') > > attributes = { > # Attribute name: (type, checkrule) > 'myattr': (int, positiveCheck), > } Just add positiveCheck = staticmethod(positiveCheck) at this point and everything should work in both Python 2 and 3. Alternatively use Steven's callable_staticmethod as a decorator. > > def __setattr__(self, name, value): > if not name in Configuration.attributes: > raise AttributeError("Attribute `%s` non allowed." %name) > > expected_type, checkrule = Configuration.attributes[name] > if not isinstance(value, expected_type): > raise TypeError('The value %s is not of type %s' \ > %(value, expected_type.__name__)) > if callable(checkrule): > print('calling %s(%s)' %(checkrule.__name__, value)) > checkrule(value) > > super(Configuration, self).__setattr__(name, value) > > The positive check works fine: > > >>> from config import Configuration > >>> c = Configuration() > >>> c.myattr = -10 > calling positiveCheck(-10) > Traceback (most recent call last): > ... > AttributeError: Must be a positive number > > But I cannot use the method as a function: > > >>> Configuration.positiveCheck(-10) > Traceback (most recent call last): > ... > Configuration instance as first argument (got int instance instead). > > Furthemore, I cannot use the method as a staticmethod, becase otherwise > it will not be callable inside the class body. PS: AttributeErrors should be raised when an attribute does not exist or cannot be set; I recommend that you raise a ValueError in positiveCheck().