Standard behaviour of a getSomething method

Dan Williams dan at osheim.org
Thu Jul 24 01:19:52 CEST 2003


Batista, Facundo wrote:
> When I want to know about a attribute (e.g.: myAttrib) of an object, I 
> should use the specific method (e.g.: getMyAttrib).
>
Its not really considered "pythonic" to access attributes through 
getters and setters. Instead, you can access them directly:

 >>> class Foo:
...     def __init__(self, val):
...             self.a = val
...
 >>> foo = Foo(4)
 >>> print foo.a
4
 >>> foo.a = 6
 >>> print foo.a
6
 >>>

If you need to to other processing of the data for the data (ie, if you 
wanted to give a copy), you can use the __getattr__ and __setattr__ 
methods.

> Considering that this attribute is always another object (everything is 
> an object in Python), what should getMyAttrib do?
> 
>         1) Return the object
>         2) Return a copy of the object
> 
> How do I return a copy of the object?
try looking at:
 >>> import copy
 >>> help(copy)

HTH,
-Dan

> 
> Thanks for all.
> 
> .       Facundo
> 
> 
[snip]





More information about the Python-list mailing list