1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Introductory python-changing classes properties

  1. Feb 8, 2012 #1
    1. The problem statement, all variables and given/known data

    Code (Text):
    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 (Text):
    >>> p.gender
    >>> p.gender = 'Female'
    This is going to cost you!
    >>> p.gender
    >>> p.gender = 'Androgyne'
    You cannot become Androgyne (no such thing)

    3. 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 (Text):
        def set_gender(self,g):
            assert g=='Male' or g=='Female','no such thing'
            print 'this is going to cost you!'
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted