Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60287
| From | Marco Buttu <marco.buttu@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Behavior of staticmethod in Python 3 |
| Date | 2013-11-23 10:39 +0100 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <529077E0.8090603@gmail.com> (permalink) |
| References | <l6povp$ndp$1@speranza.aioe.org> <mailman.3076.1385197282.18130.python-list@python.org> |
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),
}
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.
--
Marco Buttu
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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