473,322 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Metaclass v.s. Property function.

I don't like the property function, usable in the new-style classes,
because having to remember to manage a list of "foo = property(...)"
assignments just plain sucks, so I wrote a metaclass that does things
a little differently. Please have a look and tell me whether this is
useful or impractical. The metaclass is here: http://pastebin.com/m5b06b571
and some simple testcode is here: http://pastebin.com/m382f2ae9.
Notice the first line though.

Thanks for any replies,

Jens Thiede.

Aug 11 '07 #1
3 1402
Jens Thiede <je********@gmail.comwrote:
I don't like the property function, usable in the new-style classes,
because having to remember to manage a list of "foo = property(...)"
assignments just plain sucks, so I wrote a metaclass that does things
a little differently. Please have a look and tell me whether this is
useful or impractical. The metaclass is here:
http://pastebin.com/m5b06b571 and some simple testcode is here:
http://pastebin.com/m382f2ae9. Notice the first line though.
Here's something I posted a while back which lets you use property as a
decorator:

class C(object):
def __init__(self, colour):
self._colour = colour

@property.set
def colour(self, value):
self._colour = value

@property.get
def colour(self):
return self._colour

@property.delete
def colour(self):
self._colour = 'none'

See:
http://groups.google.co.uk/group/com...381be5edc26340

Whether you like that style is of course a matter of personal opinion.
Aug 11 '07 #2
On Aug 11, 7:33 am, Jens Thiede <jensthi...@gmail.comwrote:
I don't like the property function, usable in the new-style classes,
because having to remember to manage a list of "foo = property(...)"
assignments just plain sucks, so I wrote a metaclass that does things
a little differently. Please have a look and tell me whether this is
useful or impractical. The metaclass is here:http://pastebin.com/m5b06b571
and some simple testcode is here:http://pastebin.com/m382f2ae9.
Notice the first line though.
Are you overusing property, by any chance? In your example, bar seems
to be an unneeded property; it could just be a normal attribute.

Aug 11 '07 #3
On Aug 11, 6:06 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
Jens Thiede <jensthi...@gmail.comwrote:
I don't like the property function, usable in the new-style classes,
because having to remember to manage a list of "foo = property(...)"
assignments just plain sucks, so I wrote a metaclass that does things
a little differently. Please have a look and tell me whether this is
useful or impractical. The metaclass is here:
http://pastebin.com/m5b06b571and some simple testcode is here:
http://pastebin.com/m382f2ae9. Notice the first line though.

Here's something I posted a while back which lets you use property as a
decorator:

class C(object):
def __init__(self, colour):
self._colour = colour

@property.set
def colour(self, value):
self._colour = value

@property.get
def colour(self):
return self._colour

@property.delete
def colour(self):
self._colour = 'none'

See:http://groups.google.co.uk/group/com...e_thread/threa...

Whether you like that style is of course a matter of personal opinion.
Thanks, a very simple but a great and more "pythonic" idea.

Aug 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
0
by: Robin Becker | last post by:
A colleague wanted to initialize his class __new__ and tried code resembling this #######################1 class Metaclass (type): def __init__(cls, name, bases, *args, **kwargs):...
5
by: Irmen de Jong | last post by:
Hi, I've developed the Metaclass below, because I needed a way to make a bunch of classes thread-safe. I didn't want to change every method of the class by adding lock.aqcuire()..lock.release()...
33
by: Jacek Generowicz | last post by:
I would like to write a metaclass which would allow me to overload names in the definition of its instances, like this class Foo(object): __metaclass__ = OverloadingClass att = 1 att = 3
39
by: Nicolas Fleury | last post by:
In the following example: class MyMetaclass(type): pass class MyBaseType(object): __metaclass__ = MyMetaclass class MyType(MyBaseType): x = 4 y = 5 z = 6 Is there any way to modify...
1
by: Antoine Pitrou | last post by:
Hi, I've been looking at writing parameterized metaclasses and here are the two solutions I've come to: (my goal was to build a way to automatically add a hash function that would take into...
14
by: Pedro Werneck | last post by:
Hi I have a class A, with metaclass M_A, and class B, subclass of A, with metaclass M_B, subclass of M_A. A class C, subclass of B must have M_B or a subclass of it as metaclass, but what if...
9
by: Christian Eder | last post by:
Hi, I think I have discovered a problem in context of metaclasses and multiple inheritance in python 2.4, which I could finally reduce to a simple example: Look at following code: class...
2
by: lbolognini | last post by:
Hi all, I dare risk my brain exploding by reaching for the understanding of metaclasses. At first i thought i almost got them, even if vaguely back in a corner of my mind, my understanding...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.