tutorialsPythonBasic.basic.classes.class_properties module¶
- An example of using properties in a class:
- http://docs.python.org/2/library/functions.html#property
Properties have many use-cases, for instance when we want to have a class attribute that is limited to a specific value or type, but don’t want to set it using methods like .setX() or something similar.
- class tutorialsPythonBasic.basic.classes.class_properties.Person(name, age)[source]¶
Bases: builtins.object
An example Person class. You can set the name and age, with the age being limited to non-negative integers.
Let’s try to create a Person with an impossible age
>>> Person("Peter", -10) Traceback (most recent call last): ... Exception: Age must be a non-negative integer!
Now let’s try again with something viable
>>> peter = Person("Peter", 10)
Now, we could use the functions we wrote:
>>> peter.getAge() 10 >>> peter.setAge(11) >>> peter.getAge() 11
But it’s nicer and easier to use the property we defined:
>>> peter.age 11 >>> peter.age = 12 >>> peter.age 12
Of course, we still can’t set it to something that’s not possible:
>>> peter.age = 12.5 Traceback (most recent call last): ... Exception: Age must be a non-negative integer! >>> peter.age 12
What we could do (if we needed to) is cheat a bit and bypass the function, changing the “internal” variable directly (it is not actually internal or protected in any way, we just use _ in front of names to make sure people know variables like this are meant to be used by the program in the background and probably have different ways to set them up somewhere).
>>> peter._age = 'Blablabla' >>> peter.age 'Blablabla'
Of course we usually don’t want that, so let’s set it back to something viable ... oh and happy birthday Peter :)
>>> peter.age = 13 >>> peter.age 13
- age¶
The “getter”, it gets the age and returns it.