Introductory python-changing classes properties

Click For Summary
SUMMARY

This discussion focuses on redefining the gender attribute of a Python class, specifically the Person class, using properties. The solution involves implementing getter and setter methods for the gender attribute, ensuring that it can only be set to "Male" or "Female". The hidden variable _gender is used to store the gender value, which is accessed through the defined property methods. This approach prevents invalid gender assignments and maintains encapsulation within the class.

PREREQUISITES
  • Understanding of Python class definitions and object-oriented programming
  • Familiarity with Python property decorators (@property and @.setter)
  • Knowledge of assertions in Python for input validation
  • Basic understanding of encapsulation in programming
NEXT STEPS
  • Learn about Python decorators and their applications in class properties
  • Explore advanced object-oriented programming concepts in Python
  • Research best practices for input validation in Python classes
  • Study encapsulation techniques and their importance in software design
USEFUL FOR

Python developers, software engineers, and students learning object-oriented programming who want to understand class properties and encapsulation in Python.

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
 
Technology news on Phys.org


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 43 ·
2
Replies
43
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K