-
Notifications
You must be signed in to change notification settings - Fork 0
/
1objects.py
107 lines (89 loc) · 1.86 KB
/
1objects.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
#create class
class myclass:
x = 5
#create object
p1 = myclass()
print(p1.x)
print()
#the init function
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Mia", 25)
print(p1.name)
print(p1.age)
print()
#the str function
#without str
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1)
#with str
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name}({self.age})"
p1 = Person('john', '29')
print(p1)
print()
#object methods
class person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("hello, my name is" + self.name)
p1 = person(" Mia", 28)
p1.myfunc()
#self parameter
class person:
def __init__(today, name, age):
today.name = name
today.age = age
def myfunc(ain):
print("hello, my name is" + ain.name)
p1 = person(" Mia", 25)
p1.myfunc()
print()
'''#modify properties
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("Mia", 36)
p1.age = 40
print(p1.age)
#delete object properties
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("Mia", 36)
p1.age = 40
del p1.age
print(p1.age)
#delete object
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("Mia", 36)
p1.age = 40
del p1
print(p1.age)
print()
#pass statement
class person:
pass'''