Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60288
| 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 | <marco.buttu@gmail.com> |
| 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; '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; 'value))': 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:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'raise': 0.29; 'skip:@ 10': 0.30; 'object.': 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; 'received:google.com': 0.35; 'there': 0.35; 'method': 0.36; 'should': 0.36; 'positive': 0.37; 'two': 0.37; 'message-id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'name:': 0.61; 'first': 0.61; 'name': 0.63; 'body.': 0.68; 'otten': 0.84; 'not:': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:newsgroups:to:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=Pd+twl3s+VirjDlf8upGixvgjLwwvaiPiUXsQV2QY3I=; b=oVcATJfCgR6JoXZ6lnWKsDhvFv2Kvco6o7VKzmYvplLviAuRa4tfX3ltDvGzlEw2BI 5P/Da34mykPzrMr4yCCdv/JMIOls8JQPnIJpqwEWqey7Z4qohH4DHPZy9assucZWwjkP 74L6FSzU85JSqe2PjES+lLIkGuX94utW3ni6v6oXQGv5/dREyNWAAfFFa3YkTbf4F7iz MsxMtbd4o4MAN9r6yHnXJdkhiDCQcp+Bz3K6j/YZ3qpSvLuumvh9lmmYRzYWLV5pUFzE 4wdoK7zjMi4xCH2YY4WrdNkKNR74s6HzE0uyNioaUJ7kqYFk4oGjFHPJ2X7hODISdes+ bq7Q== |
| X-Received | by 10.15.91.3 with SMTP id r3mr22453273eez.18.1385199588671; Sat, 23 Nov 2013 01:39:48 -0800 (PST) |
| Date | Sat, 23 Nov 2013 10:39:44 +0100 |
| From | Marco Buttu <marco.buttu@gmail.com> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 |
| MIME-Version | 1.0 |
| Newsgroups | comp.lang.python |
| To | python-list@python.org |
| Subject | Re: Behavior of staticmethod in Python 3 |
| References | <l6povp$ndp$1@speranza.aioe.org> <mailman.3076.1385197282.18130.python-list@python.org> |
| In-Reply-To | <mailman.3076.1385197282.18130.python-list@python.org> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| 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> |
| Message-ID | <mailman.3078.1385199596.18130.python-list@python.org> (permalink) |
| Lines | 81 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1385199596 news.xs4all.nl 15925 [2001:888:2000:d::a6]:36703 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:60288 |
Show key headers only | View raw
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