|
From: Guy K. K. <guy...@au...> - 2011-03-02 06:35:01
|
On Wed, 02 Mar 2011 06:45:04 Bruce Sherwood wrote:
> In vis/primitives.py, the code for
> changing color and material for a text object should look like this:
>
> def set_material(self, material):
> self.extrusion.material = material
> def get_material(self):
> return self.__material
>
> def set_color(self, color):
> self.extrusion.color = color
> def get_color(self):
> return self.__color
Are you *really* sure about that? Python usually does not use the concept of
getters and setters. This turns out to be *very* unpythonic. I'd rather
suggest the following:
@material.setter
def material(self, material):
self.extrusion.material = material
@property
def material(self):
return self.__material
@color.setter
def set_color(self, color):
self.extrusion.color = color
@property
def get_color(self):
return self.__color
This exposes the given methods to transparently handle access (setting and
getting) to mock attributes as they would be normally used within Python
classes. It makes a nice API that is idiomatically consistent to use for a
Pythoneer.
For further reference on how to use the property() built-in, particularly in
the way of decorators, have a look here:
http://docs.python.org/library/functions.html#property
Guy
--
Guy K. Kloss
School of Computing + Mathematical Sciences
Auckland University of Technology
Private Bag 92006, Auckland 1142
phone: +64 9 921 9999 ext. 5032
eMail: Guy...@au...
|