Mumfordwiz
Mumfordwiz

Reputation: 1545

different setters for python properties

I have a class

class Animal:
    def __init__(self, name='', num_of_owners=0, sleep=0):
        self.name = name
        self.num_of_owners = int(float(num_of_owners))
        self.sleep = float(sleep)

let's say I'm reading all the properties from some file.

I'm using Properties for getters and setters.

@property
def name(self):
    return self.name

@name.setter
def name(self, value):
    self.name = value

now when reading from the file, I don't want to look for every property in the dictionary i got specifically.

So i can run a for over the dictionary and type

for name, value in animal_props.iteritems():
     setattr(animal, name, value)

but then all the properties are set as strings. The thing is I have about 8 properties some floats some int some strings.

Anyway to run this for, and not make regular setters and run a specific setter for each property.

example:

class Animal:
    def __init__(self, name='', num_of_owners=0, sleep=0):
        self._name = name
        self._num_of_owners = int(float(num_of_owners))
        self._sleep = float(sleep)

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self._name = value

    @property
    def num_of_owners(self):
        return self._num_of_owners

    @num_of_owners.setter
    def num_of_owners(self, value):
        self._num_of_owners = int(value)

    @property
    def sleep (self):
        return self._sleep

    @sleep.setter
    def sleep(self, value):
        self._sleep = int(float(value))

d = {'name': 'doggy', 'num_of_owners': '3', 'sleep': '5.643'}
dog = Animal()
for name, value in d.iteritems():
    setattr(dog, name, value)

print type(dog.sleep)

I need the type at the end to be float. since i will later use it as a float.

Creating separate 'ifs' and send to each setter is fine, but is there anyway to do it with just that one for.

Upvotes: 0

Views: 790

Answers (2)

Blckknght
Blckknght

Reputation: 104712

If you're really using Python 2 (as your tag suggests), you need to change your class declaration so that you inherit from object. Doing so will make your class a "new style" class rather than an "old-style" class (which were the only kind of class back in the Python 2.1 days). If you don't know much about these two kinds of class, don't worry about learning about the old ones. Just create new-style classes always (they're the only kind in Python 3).

To make a new-style class, inherit from object:

class Animal(object):
    #...

Note that if you're not doing type conversions or other kinds of validation in your property getter or setter methods (as with name in your example code), you might as well get rid of the whole property and just use a regular attribute instead. If you find you do need validation later on in your program's design, you can switch back to using a property at that point and the other parts of the code that read or write from the attribute won't need to change.

Upvotes: 1

Daniel
Daniel

Reputation: 42748

You are using python 2 with old-style classes. Properties are only available with new-style classes:

class Animal(object):
    ...

Upvotes: 1

Related Questions