-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSuper Function Examples.py
More file actions
94 lines (71 loc) · 3.44 KB
/
Super Function Examples.py
File metadata and controls
94 lines (71 loc) · 3.44 KB
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
94
# Let's learn what the 'super()' function does, when we want to use
# attributes from the Main class, if we want all our classes to be the
# same. Let's use the 'super()' function to get rid of redundant attribute
# code blocks, since they are all the same. Let's get real lazy now.
# HIGHLIGHT AND COPY CODE, THEN PASTE INTO YOUR PREFERABLE PYTHON APP/IDLE
class Main_class:
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
class Sub_class1(Main_class): # class inheritance variable
def __init__(self,fname,lname,age):
super().__init__(fname,lname,age) # super() calls the __init__ method
class Sub_class2(Main_class): # class inheritance variable
def __init__(self,fname,lname,age):
super().__init__(fname,lname,age)
print(Main_class('John','Smith',23).first_name)
print(Sub_class1('John','Smith',23).last_name)
print(Sub_class2('John','Smith',23).age)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Main_class:
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print(self.first_name)
class Sub_class1(Main_class): # class inheritance variable
def __init__(self,fname,lname,age):
super().__init__(fname,lname,age) # super() calls the __init__ method
print(self.last_name)
class Sub_class2(Main_class): # class inheritance variable
def __init__(self,fname,lname,age):
super().__init__(fname,lname,age)
print(self.age)
Main_class('John','Smith',23)
Sub_class1('John','Smith',23)
Sub_class2('John','Smith',23)
# Notice how everything is exactly the same, except for the attributes
# and the super() function, which allows you to get rid of redundant attribute
# code blocks? Now you can use the super() function with classes.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's use the super() function with some of the attributes that we will
# need with the others with different attribute names. We will also create
# each class to have their very own attribute characteristics.
class Parent_main:
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print(self.first_name)
class Sub_child1(Parent_main): # class inheritance variable
def __init__(self,fname,lname,age,dog,cat,bird,fish):
super().__init__(fname,lname,age)
self.dog=dog
self.cat=cat
self.bird=bird
self.fish=fish
print(self.first_name,'I love my',self.dog,self.cat,'and my',self.bird)
class Sub_child2(Parent_main): # class inheritance variable
def __init__(self,fname,lname,age,hair,colour,eyes):
super().__init__(fname,lname,age)
self.hair=hair
self.colour=colour
self.eyes=eyes
print(self.first_name,'I have long',self.colour,self.hair,'and',self.eyes)
Parent_main('John','Smith',23)
Sub_child1('John','Smith',23,'Dog','Cat','Bird','Fish')
Sub_child2('Jane','Smith',23,'hair','blond','huge blue eyes.')
print(Main_class('John','Smith',23).first_name)
print(Sub_class1('John','Smith',23).last_name)
print(Sub_class2('John','Smith',23).age)