need help with properties
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sat May 9 11:56:37 EDT 2009
On Sat, 09 May 2009 11:30:02 -0400, Esmail wrote:
> Hi,
>
> I am just reading about properties in Python. I am thinking of this as
> an indirection mechanism, is that wrong? If so, how come the
> getter/setters aren't called when I use properties instead of the
> functions directly?
>
> What am I missing here? I have a feeling I am overlooking something
> simple here...
Sure are.
In your test code, you have:
> r = Rectangle()
> print r.getSize()
> r.setSize(-40, 30)
> print r.getSize()
>
> print '-----------------'
> size = 3, 7
> print size
All you've done in this second block is define a new object called "size"
and assign the tuple (3, 7) to it. [Aside: you might not be aware that it
is commas that make tuples, not brackets. The brackets are for grouping.)
"size" has nothing to do with your Rectangle class, or the property.
Apart from the accidental similarity of name between "Rectangle.size" and
"size", it's unrelated to the property you created.
What you probably intended to do was this:
print '-----------------'
r.size = 3, 7
print r.size
In other words, your property is attached to the Rectangle instance.
--
Steven
More information about the Python-list
mailing list