Introductory python-changing classes properties

In summary: The property() function is used to define class properties, which are attributes that have getter and setter methods.
  • #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
 
Technology news on Phys.org
  • #2


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".
 

1. What is an introductory python-changing class property?

An introductory python-changing class property refers to the ability to modify the properties or attributes of a class in Python. This allows for greater customization and flexibility when creating objects from a class.

2. How do you change class properties in python?

To change class properties in python, you can use the setattr() function, which takes in the class instance, the property name, and the new value as parameters. Alternatively, you can also directly assign a new value to the property using dot notation.

3. Why would you want to change class properties in python?

Changing class properties in python allows for more dynamic and adaptable code. It can be useful when creating different instances of a class that require slightly different properties or when responding to changes in the program's environment.

4. Are there any limitations to changing class properties in python?

One limitation is that not all class properties can be changed. Some properties may be read-only, meaning they cannot be modified once the class is created. Additionally, changing class properties may also impact the functionality of other parts of the code, so careful consideration should be taken when making changes.

5. Can you change the properties of a built-in python class?

No, the properties of built-in python classes cannot be changed as they are predefined and cannot be modified. However, you can subclass a built-in class and add your own properties to the subclass.

Similar threads

  • Programming and Computer Science
2
Replies
43
Views
2K
  • Programming and Computer Science
Replies
9
Views
902
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Classical Physics
Replies
19
Views
2K
Back
Top