-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass and objects.py
152 lines (101 loc) · 2.8 KB
/
class and objects.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# PYTHON OOPS
# CLASS AND OBJECT
from cairo import STATUS_INVALID_CONTENT
class demo():
pass
a = 10
print(type(a))
# it will print class 'int'
# class is a type
print(type(demo))
# it will print class 'type'
print('--------------------------------------')
class car():
pass
a = 10
swift=car()
# swift is object
# car is class
print(isinstance(a,int))
print(isinstance(swift,car))
print(type(swift))
# it will print as class '__main__.car'
# here main programming special variable car inside the class is swift
#########################################################
# class Attributes
class student():
name = 'rock'
age = 19
# get attribute = getattr() method
# we can pass 3 argument in attribute
print(getattr(student, 'name'))
print(getattr(student, 'age'))
# it will give error attribute error
print(getattr(student, 'code','i luv python'))
# now no error will occur
# dot notation (.)
print(student.name)
print(student.age)
#setattr setting attribute
setattr(student,'name','! R O C K ')
print(student.name)
# it will change
# we can create , change , delete in setattr
setattr(student,'code','python')
print(student.code)
student.city='tvl'
print(student.city)
# we can create dot notation object and we can print
print(student.__dict__)
print( ' - - - - - - - - - - - - - - - -- - ')
delattr(student, 'city')
print(student.__dict__)
print( ' - - - - - - - - - - - - - - - -- - ')
del student.code
print(student.__dict__)
print( ' - - - - - - - - - - - - - - - -- - ')
print( ' - - - - - - - - - - - - - - - -- - ')
print( ' - - - - - - - - - - - - - - - -- - ')
#################################
# instance attributes
class user():
course = 'java'
o = user()
print(user.__dict__)
print(user.course) # printing class attribute
print(o.__dict__)
print(o.course)
o.course = 'c++'
print(o.course)
print(o.__dict__)
# but it can't change the class attribute that means java
o2 = user()
print(o2.course)
# if we want instance attribute .. we can create a dict value
# but it can't change the main attribute with instance attribute
##############################################
# class methods
class meb():
name = 'rock'
age = 19
def printall():
print('name :',meb.name)
print('age :',meb.age)
meb.printall()
print(meb.printall)
print(meb.__dict__)
#################################################
# instance methods
# if we want to call a instance methods
# class function and object we have to call .. is the procedure to call instance methods
class mebs():
name = 'rock'
age = 19
def printall(self,code):
print('name :',mebs.name)
print('age :',mebs.age)
print('code : ',code)
o = mebs()
o.printall('python')
mebs.printall(o,'python')
#################################################