-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path059-super.py
More file actions
26 lines (23 loc) · 876 Bytes
/
Copy path059-super.py
File metadata and controls
26 lines (23 loc) · 876 Bytes
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
"""
Inherit all methods and properties from another class
-----------------------------------------------------
A class (child class or derived class) inherits properties, methods and
functions from another class (parent class or base class).
"""
# Parent class
class Person:
def __init__(self, firstname, lastname, age):
self.firstname = firstname
self.lastname = lastname
self.age = age
def printpersondata(self):
print('Firstname: {}\nLastname: {}\nAge: {}\n'
.format(self.firstname, self.lastname, self.age))
# Child class
# This class inherits printpersondata() from Person class
class Worker(Person):
def __init__(self, firstname, lastname, age):
super().__init__(firstname, lastname, age)
# Create a Worker
person_1 = Worker('Alice', 'Smith', '36')
person_1.printpersondata()