-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_02_object_person.py
executable file
·93 lines (77 loc) · 2.25 KB
/
example_02_object_person.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# IMPORT MODULE
# annotations enables you to use the class Person as a data type inside the class itself
from __future__ import annotations
class Person:
"""
This class stores information about a person
Methods
-------
__init__(self, name, surname, age)
getAge()
getFullName()
setSurname(surname)
greet(other)
"""
def __init__(self, name: str, surname: str, age: int):
"""
Create an instance of the Person class
Parameters
----------
name : str
Name of the person.
surname : str
Surname of the person.
age : int
The persons age.
"""
# Save the parameters of the methods as attributes of the class
self.name = str(name).strip()
self.surname = str(surname).strip()
self.age = int(age)
def getAge(self) -> int:
"""
Return the age of a person.
Returns
-------
age : int
The age of the person.
"""
return self.age
def getFullName(self) -> str:
"""
Return the full name of a person.
Returns
-------
str
The full name of the person.
"""
return " ".join([self.name, self.surname])
def setSurname(self, new_surname: str):
"""
Change the surname of a person.
Parameters
----------
new_surname : str
The new surname of the person.
"""
self.surname = str(new_surname).strip()
def greetYou(self, other: Person):
"""
Greet another person.
Parameters
----------
other : Person
An instance of the Person class.
"""
print(f"Hi, {other.getFullName()}! My name is {self.getFullName()}.")
if __name__ == "__main__":
# Create instances of the class
person1 = Person("Marie", "Mustermann", 22)
person2 = Person("Max", "Mustermann", 20)
# Show some properties of the class
print(f"Call method: {person1.getFullName()}")
person1.setSurname("Müller")
person2.greetYou(person1)
print(f"It is possible to access attributes: {person2.name}")