Question: Create an instance of the Person class from exercise 1. Use the dir function on the instance. Then use the dir function on the class.
Create an instance of the Person class from exercise 1. Use the dir function on the instance. Then use the dir function on the class.
- What happens if you call the __str__ method on the instance? Verify that you get the same result if you call the str function with the instance as a parameter.
- What is the type of the instance?
- What is the type of the class?
- Write a function which prints out the names and values of all the custom attributes of any object that is passed in as a parameter. (see vars() hint.)
Dir() Hint:
person = Person() print(dir(person))
vars() Hint:
print(vars(person))
Person Class definition
import datetime class Person: def __init__(self, name, surname, birthdate, address, telephone, email): self.name = name self.surname = surname self.birthdate = birthdate self.address = address self.telephone = telephone self.email = email def age(self): today = datetime.date.today() age = today.year - self.birthdate.year if today < datetime.date(today.year, self.birthdate.month, self.birthdate.day): age -= 1 return age person = Person( "Jane", "Doe", datetime.date(1992, 3, 12), # year, month, day "No. 12 Short Street, Greenville", "555 456 0987", "jane.doe@example.com" ) print(person.name) print(person.email) print(person.age())
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
