Implementing class attribute access methods via pseudo-function o verloading.

Diez B. Roggisch deetsNOSPAM at web.de
Mon Oct 25 07:25:26 EDT 2004


> Am I correct in stating that Python does not support function overloading?
> Have I been hiding under a rock and not noticed the thousands of examples
> that illustrate this approach?

No, you are basically right - there is no implicit type-based dispatch in
python. There are some recipes for  that, but basically they do the same
you to - check for arguments types, and dispatch accordingly

> Any comments are welcome!

Why don't you use properties? That even spares you the () for accessing
variables, and allows direct left-hand-side assignment:

class Foo(object):
    def __init__(self):
        self._bar = "bar"

    def _g_bar(self):
        return self._bar

    def _s_bar(self, value):
        self._bar = value

    bar = property(_g_bar, _s_bar)

f = Foo()

print f.bar
f.bar = "baz"

print f.bar


I really prefer that over all other mechanisms, and it allows for bar not
beeing simply stored in the object itself, but instead e.g. fetched lazily
from a database or received/transmitted over a network.

-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list