-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_self variable.py
More file actions
70 lines (53 loc) · 1.55 KB
/
02_self variable.py
File metadata and controls
70 lines (53 loc) · 1.55 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
# create a class student having 3 attributes and 1 methods.
#1)
class Student :
def __init__(self) :
self.name = input("Enter your name : ")
self.age = int(input("Enter your age : "))
self.course = input("Enter your course : ")
def printDetails(self) :
print("-"*30)
print(f"name is {self.name}")
print(f"age is {self.age}")
print(f"course is {self.course}")
s1 = Student()
s1.printDetails()
#2)
class Student :
def __init__(self) :
self.name = input("Enter your name : ")
self.age = int(input("Enter your age : "))
self.course = input("Enter your course : ")
def printDetails(self) :
print("-"*30)
print(f"name is {self.name}")
print(f"age is {self.age}")
print(f"course is {self.course}")
s1 = Student()
s2 = Student()
s3 = Student()
s1.printDetails()
s2.printDetails()
s3.printDetails()
# 3) Here self and s1 pointing to same memory location.
class student :
def __init__(self) :
print(id(self))
s1 = Student()
print(id(s1))
print("-"*20)
s2 = Student()
print(id(s2))
# 4) self is not a keyword.
class Student :
def __init__(x) :
x.name = input("Enter your name : ")
x.age = int(input("Enter your age : "))
x.course = input("Enter your course : ")
def printDetails(x) :
print("-"*30)
print(f"name is {x.name}")
print(f"age is {x.age}")
print(f"course is {x.course}")
s1 = Student()
s1.printDetails()