Introductory python-changing classes properties

obnoxiousris
Messages
20
Reaction score
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
 


To change the gender of an existing person object, you can redefine the gender attribute as a property with getter and setter methods. This will allow you to control the change and make sure the new gender is either "Male" or "Female". To do this, you can use the property() function and define getter and setter methods for the gender attribute:

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

@property
def gender(self):
return self._gender

@gender.setter
def gender(self, new_gender):
assert new_gender == "Male" or new_gender == "Female", "no such thing"
print("This is going to cost you!")
self._gender = new_gender

Now, when you create a person object, you can change their gender by simply using the assignment operator:

p = Person("19900405", "Male", "John")
p.gender = "Female"
print(p.gender) # prints "Female"

The hidden variable in this case is the _gender variable, which is accessed through the getter and setter methods. This way, the gender attribute cannot be directly changed outside of the class, ensuring that the new gender is always "Male" or "Female".
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 25 ·
Replies
25
Views
7K