FEEDBACK WANTED: Type/class unification

Guido van Rossum guido at python.org
Tue Jul 31 13:44:59 EDT 2001


Martin von Loewis <loewis at informatik.hu-berlin.de> writes:

> By default, instance attributes are not accessible outside of instance
> methods. In instance methods, attributes are denoted by an initial
> ampersand (e.g. @age, @name, @counter). You can declare attributes as
> accessible, using
[etc.]

(Thanks for the explanation.)

It seems that because of the access rules, this makes sense for Ruby.
But since attrs are public by default in Python, it makes less sense
here.  We could easily have a shortcut that creates a pair of accessor
functions to map a public attribute name to a private attribute, but
what good would it do us?  You only need to use accessor functions
when you want additional functionality.  I guess a read-only attribute
is the simplest added functionality.  Thinking aloud:

def readonly(name):
    def get(obj):
        return getattr(obj, '_' + name)
    return getset(get, None)

class C:

    foo = readonly('foo')

    def __init__(self):
        self._foo = 12 # access raw attribute

x = C()
print x.foo # legit
x.foo = 21 # error

--Guido van Rossum (home page: http://www.python.org/~guido/)




More information about the Python-list mailing list