class methods vs. functions

Christopher T King squirrel at WPI.EDU
Thu Jul 15 14:10:23 EDT 2004


On Thu, 15 Jul 2004, Egbert Bouwman wrote:

> My impression is that you can hardly call this OOO:
> Object Oriented Orthodoxy. For instance, from the GOF I learned:
> - the object's internal state can not be accessed directly
> - operations are the only way to change an object's internal state
> - requests (or messages) are the only way to get an object 
>   to execute an operation
> - a signature consists of an operation's name, parameters and return value
> - the interface to an object is the set of all signatures of its operations

Replace .x with .getx() and .y with .gety() if it suits you, but often in 
the case where an object is mostly used as a container (similar to a C 
struct), direct access to its attributes is considered the norm:

a = 3+5j
a.real      # --> 3.0
a.imag      # --> 5.0

If you so choose, you can make .x and .y read-only with a little
__setattr__ magic (there's probably an easier way to do this in new-style
classes), as they are in the complex type:

a.real = 4  # --> AttributeError

Sure, it may not fit with "proper OO methodology", but it's a good way of
making visible the fact that "this object is a container, I'm retrieving
one of the variables it contains".  If the interface is well-defined, it
doesn't matter how you access it, so long as you follow the interface's 
design.




More information about the Python-list mailing list