need help with properties

E

Esmail

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...

Thanks,
Esmail

---
#!/usr/bin/env python

#
# quick test to deal with 'private' attributes and
# python properties ...
#

import sys

class Rectangle(object):

def __init__(self):
self.__width = 0
self.__height = 0


def setSize(self, width, height):
print 'in setter'
if (width < 0) or (height < 0):
print >> sys.stderr, 'Negative values not allowed'
else:
self.__width = width
self.__height = height


def getSize(self):
print 'in getter'
return self.__width, self.__height


size = property(getSize, setSize)

r = Rectangle()
print r.getSize()
r.setSize(-40, 30)
print r.getSize()

print '-----------------'
size = 3, 7
print size

print '-----------------'
size = -3,7
print size



The program output:


in getter
(0, 0)
in setter
Negative values not allowed
in getter
(0, 0)
 
S

Steven D'Aprano

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.
 
E

Esmail

hi Scott,
Because you weren't actually using them. You were writing:
size = 3, 7
not:
r.size = 3, 7

duh!! .. I knew it had to be something sill .. how do I represent
a red face in ASCII ? ;) I was concentrating so much on the new
feature I totally didn't see this.

I appreciate your code with annotations, helpful.
def setSize(self, shape): # a setter takes a single arg
width, height = shape
print 'in setter'
if width < 0 or height < 0:
print >> sys.stderr, 'Negative values not allowed'
else:
self._width = width
self._height = height
r.setSize((-40, 30))

Just curious, is there a way to pass more than one arg to a setter, or
do we always have use some sort of unpacking at the other end?

Thanks,

Esmail
 
E

Esmail

Steven said:
All you've done in this second block is define a new object called "size"
and assign the tuple (3, 7) to it.

oops .. yes, you are right, and I am embarrassed...

[Aside: you might not be aware that it
is commas that make tuples, not brackets. The brackets are for grouping.)

Helpful clarification, thanks!
"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

Yup, I know Python is a dynamically typed language, but I wish it would
point this sort of silliness out .. but there are tradeoffs. I should
probably run pyflakes/pychecker/pylint on my my tiny test/try out codes
too.

Best,
Esmail
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top