Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Classes can also have references to other instances of themselves. Consider this Person class, for example, that allows for an instance of a father and mother to be given in the constructor.
- Create 3 instances of this class. The first should have the name "Mr. Burdell" with an age of 53.
- The second instance should have a name of "Mrs. Burdell" with an age of 53 as well.
- Finally, make an instance with the name of "George P. Burdell" with an age of 25. This final instance should also have the father attribute set to the instance of Mr. Burdell, and the mother attribute set to the instance of Mrs. Burdell. Finally, store the instance of George P. Burdell in a variable called george_p. (It does not matter what variable names you use for Mr. and Mrs. Burdell.)
- '''
- class Person:
- def __init__(self, name, age, father=None, mother=None):
- self.name = name
- self.age = age
- self.father = father
- self.mother = mother
- #My code
- p1 = Person("Mr. Burdell", 53)
- p2 = Person("Mrs. Burdell", 53)
- george_p = Person("George P. Burdell", 25, p1, p2)
- #Sample model answer that can be all done in one line
- #george_p = Person("George P. Burdell", 25, mother = Person("Mrs. Burdell", 53), father = Person("Mr. Burdell", 53))
- #The code below will let you test your code. It isn't used for grading, so feel free to modify it. As written, it should print George P. Burdell, Mrs. Burdell, and Mr. Burdell each on a separate line.
- print(george_p.name)
- print(george_p.mother.name)
- print(george_p.father.name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement