[Python-au] New Python Class Features

Andrew Bennetts andrew-pythonau@puzzling.org
Fri, 17 Jan 2003 15:04:59 +1100


On Fri, Jan 17, 2003 at 02:39:16PM +1100, Graeme Matthew wrote:
> 
> 
> Hi all
> 
> When designing Classes does one have to set getters and setters

Generally you don't bother.

----
class Order(object):
    def __init__(self):
        self.order = None

o = Order()
o.order = 'foo'
print o.order
----

> e.g.
> 
> class Order(object):
> 
>    def __init__ (self):
>       self.__order = None
> 
>    def get_order(self):
>        return self.__order
> 
>    def set_order(self, val):
>        self.__order = val
> 
>    order = property(get_order, set_order)

I'd recommend only using property for computed attributes (i.e. ones that
need a function call to calculate their value).  Most of the time you don't
need them.

> What I am trying to determine if their is a short way as this is going to 
> take ages when writing an object that has 20 properties

Indeed :)

Just use the simplest thing that works -- accessing the attributes directly.
Is there any particular reason why you wanted to use properties?

-Andrew.