- #1
obnoxiousris
- 21
- 0
Homework Statement
Code:
class Person(object):
"""Defines a person with dob (date-of-birth, string), gender
(two-value string), name (string) and age (integer)."""
def __init__(self, dob, gender, name):
assert (isinstance(dob,str) and dob.isdigit()
and len(dob) == 8), 'dob is not in the format yyyymmdd'
assert (gender == "Male" or
gender == "Female"), 'gender can be only "Male" or "Female"'
super(Person, self).__init__()
self._dob = dob
self.gender = gender
self.name = name
make a bold step and allow an already existing person object to change its gender
(Male to Female or Female to Male, but no other) by redefining the gender attribute
as a property with getter and setter aspects (the gender value should be stored
in a hidden variable, for example called _gen). Once redefined, the person object
gender change will be controlled:
Code:
>>> p.gender
'Male'
>>> p.gender = 'Female'
This is going to cost you!
>>> p.gender
'Female'
>>> p.gender = 'Androgyne'
You cannot become Androgyne (no such thing)
The Attempt at a Solution
i made a method within the class definition. when called, the method will check the gender then print the line. but i don't know how to do this without defining a method. and i also don't get the hidden variable thing, because it can be accessed out side the class anyways. lastly, what is a class property? how do i change class attributes into class properties? there is this property() function, but I'm not really sure its relevancy.
Code:
def set_gender(self,g):
assert g=='Male' or g=='Female','no such thing'
print 'this is going to cost you!'
self.gender=g