-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass object attribute .py
121 lines (51 loc) · 1.72 KB
/
Class object attribute .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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# class keyword to make classes and starts with capital for chosing class name
class Sample():
pass
# In[2]:
sample = Sample()
# In[3]:
type(sample)
# we can see that this sample instance is a Sample type
# In[60]:
# __init function call whenever create the instance its like a constructor
# self keyword is use for that this function is belong to the current class like getting context of a class or its a reference of the class
class Dog():
# Class object attribure
# Same in any instance of a class
# no need self keyword because self is a reference of a specific instance
species = 'Mammal'
def __init__(self,bread,name,age,isGerman):
# assign it using self.attribute name
# you can take any of name in paramater like bread1 and self.bread = bread1 it will work!
self.bread = bread
self.name = name
self.age = str (age) + " months"
self.isGerman = isGerman
# Operations / Actions -> Methods
# eg in case of Dog is bark()
def bark(self):
print('Wooof ! my name is {}'.format(self.name))
# In[ ]:
# In[61]:
# here is making instance of Class Dog and you can get by attribute by ' . '
my_dog = Dog("bread","tommy",6,False)
# In[62]:
my_dog.species
# In[63]:
my_dog.bark()
# In[ ]:
# In[64]:
# example with default value of an attribute
class Circle():
pi_val = 3.14
def __init__(self,radius = 1):
self.radius = radius
# In[65]:
# when you using default value of an attribute than not necessory to give attribute val while making instance
circle = Circle()
# In[66]:
circle.radius
# In[ ]: