Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19258
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <arnodel@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.012 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'example:': 0.03; '"my': 0.09; "'my": 0.09; 'def': 0.13; 'received:209.85.214.174': 0.13; 'a()': 0.16; 'a.x': 0.16; 'b()': 0.16; 'obj,': 0.16; 'test()': 0.16; 'val': 0.16; '>>>': 0.18; 'occurred': 0.18; 'slightly': 0.19; 'all,': 0.27; 'message-id:@mail.gmail.com': 0.28; 'pass': 0.28; 'skip:p 30': 0.29; 'class': 0.29; 'received:209.85.214': 0.32; 'implement': 0.32; 'to:addr:python-list': 0.33; 'setting': 0.34; 'properties': 0.36; 'none': 0.36; 'but': 0.37; "there's": 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.37; 'allows': 0.38; 'received:209.85': 0.38; 'why': 0.39; 'else': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.40; 'property': 0.62; 'here': 0.64; 'subject:properties': 0.84; 'subject:write': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=+qXaYPPwDR7vW2qQVp7eN4PjO4J1cg6Yvfc75OD0mzE=; b=ZaOcjbfWxxjF2GtNM+6TnkRSchx8CIljbfuUuwleh6LLzKXVC2KqEcYToiRPdqIf+S YT5lQzJYp+bNIrFfmE+5zTPMOj5SgJ+ybo5m01OeqN+sZ/Y+9YrPCA9zW4rms1E+dVxc ruv0seiRgusMbElNg6EtsFvUj99yYjWoq0hDc= |
| MIME-Version | 1.0 |
| Date | Mon, 23 Jan 2012 11:45:11 +0000 |
| Subject | A way to write properties |
| From | Arnaud Delobelle <arnodel@gmail.com> |
| To | Python <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4959.1327319113.27778.python-list@python.org> (permalink) |
| Lines | 70 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1327319113 news.xs4all.nl 6909 [2001:888:2000:d::a6]:55741 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:19258 |
Show key headers only | View raw
Hi all,
It just occurred to me that there's a very simple but slightly
different way to implement properties:
class PropertyType(type):
def __get__(self, obj, objtype):
return self if obj is None else self.get(obj)
def __set__(self, obj, val):
self.set(obj, val)
def __delete__(self, obj):
self.delete(obj)
class Property(metaclass=PropertyType):
pass
# Here is an example:
class Test:
class x(Property):
"My property"
def get(self):
return "Test.x"
def set(self, val):
print("Setting Test.x to", val)
# This gives:
>>> t = Test()
>>> t.x
'Test.x'
>>> t.x = 42
Setting Test.x to 42
>>> Test.x
<property 'x'>
>>> Test.x.__doc__
'My property'
It also allows defining properties outside class scopes:
class XPlus1(Property):
"My X Property + 1"
def get(self):
return self.x + 1
def set(self, val):
self.x = val - 1
class A:
def __init__(self):
self.x = 0
x_plus_one = XPlus1
class B:
def __init__(self):
self.x = 2
x_plus_one = XPlus1
>>> a = A()
>>> b = B()
>>> a.x
0
>>> a.x_plus_one
1
>>> b.x_plus_one
3
I don't know why one would want to do this though :)
--
Arnaud
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
A way to write properties Arnaud Delobelle <arnodel@gmail.com> - 2012-01-23 11:45 +0000 Re: A way to write properties HEK <elkarouh@gmail.com> - 2012-01-24 01:51 -0800
csiph-web