pythonic use of properties?

M

Marcus Goldfish

I'd like advice/opinions on when it is appropriate to do
attribute/property validation in python. I'm coming from a C#/Java
background, where of course tons of "wasted" code is devoted to
property validation. Here is a toy example illustrating my question:

# Example: mixing instance attributes with properties. Is it pythonic to
# validate property data in setters? Since tens and ones are never
# validated, the class can be "broken" by setting these directly
class SillyDecimal(object):
"""A silly class to represent an integer from 0 - 99."""
def __init__(self, arg=17):
if isinstance(arg, tuple):
self.tens = arg[0]
self.ones = arg[1]
else:
self.number = arg

def getNumber(self):
return self.tens*10 + self.ones
def setNumber(self, value):
if value < 0 or value > 99:
raise ArgumentException("Must in [0, 99]")
self.tens = value // 10
self.ones = value % 10
number = property(getNumber, setNumber, None, "Complete number, [0-99]")

x = SillyDecimal()
x.number, x.tens, x.ones # returns (17, 7, 1)

Even though "tens", "ones" and "number" all appear as attributes, only
"number" has its input validated. Since the class is designed to only
hold numbers 0 - 99, one can 'break' it by setting self.tens=11, for
example. Should tens and ones be made into full-fledged properties
and validated? Should number have no validation? Is it more pythonic
to encapsulate tightly, or rely on "responsible use."

Marcus
 
M

Michele Simionato

I don't use properties that much, but when I use them
I put them *outside* the class, in a property factory.
Here is an example I have prepared for my course at
PyUK, using a property to crypt a password attribute:

class User(object):
def __init__(self, username, password):
self.username, self.password = username, password


def cryptedAttribute(seed="x"):
def get(self):
return getattr(self, "_pw", None)
def set(self, value):
self._pw = crypt(value, seed)
return property(get, set)


User.pw = cryptedAttribute()

I feel that:

1) separation of concerns is of the utmost importance;
2) classes should be kept as short as possible.

Notice that in this design getters and setters are
really hidden from the user, which may be or may be
not what you want.

Michele Simionato
 
D

Diez B. Roggisch

Even though "tens", "ones" and "number" all appear as attributes, only
"number" has its input validated. Since the class is designed to only
hold numbers 0 - 99, one can 'break' it by setting self.tens=11, for
example. Should tens and ones be made into full-fledged properties
and validated? Should number have no validation? Is it more pythonic
to encapsulate tightly, or rely on "responsible use."

You could make them double-underscored attributes. That creates some
name-mangling that prevents accidential access. But as there is no real
private concept in python (and java and C++ protection can be easily
overcome), it is considered pythonic to rely on responsible use. If a
design for abuse-protection is the ultimate goal, the common suggestion is
to use IPC mechanisms to prevent in-process sharing of data and code -
that's the only real way to go, regardless of the used language.
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top