-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobj.py
More file actions
52 lines (34 loc) · 1.04 KB
/
obj.py
File metadata and controls
52 lines (34 loc) · 1.04 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
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
# Example 2
# it is clearly seen that self and obj is refering to the same object
class check:
def __init__(self):
print("Address of self = ", id(self))
obj = check()
print("Address of class object = ", id(obj))
# this code is Contributed by Samyak Jain
class Car:
def __init__(self, model, color):
self.model = model
self.color = color
def show(self):
print("Model is", self.model)
print("Color is", self.color)
c1 = Car("Audi", "Blue")
c2 = Car("BmW", "red")
c1.show()