Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19258 > unrolled thread
| Started by | Arnaud Delobelle <arnodel@gmail.com> |
|---|---|
| First post | 2012-01-23 11:45 +0000 |
| Last post | 2012-01-24 01:51 -0800 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
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
| From | Arnaud Delobelle <arnodel@gmail.com> |
|---|---|
| Date | 2012-01-23 11:45 +0000 |
| Subject | A way to write properties |
| Message-ID | <mailman.4959.1327319113.27778.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | HEK <elkarouh@gmail.com> |
|---|---|
| Date | 2012-01-24 01:51 -0800 |
| Message-ID | <57b1ea37-c86b-452c-82a1-395d1b2c9b69@i18g2000yqf.googlegroups.com> |
| In reply to | #19258 |
On Jan 23, 12:45 pm, Arnaud Delobelle <arno...@gmail.com> wrote:
> 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
Nice idea.
What would be the python2.7 version (adding __metaclass__=PropertyType
didn't help) ?
Many thanks
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web