-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_class_object.py
More file actions
91 lines (58 loc) · 1.81 KB
/
python_class_object.py
File metadata and controls
91 lines (58 loc) · 1.81 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Hello World program in Python
print("Python Classes and Objects")
# Create a Class
# To create a class, use the keyword class:
class MyClass:
x = 5
print(MyClass)
# Create Object
# Now we can use the class named myClass to create objects:
class MyClass:
x = 5
p1 = MyClass()
print(p1.x)
# The __init__() Function
# Create a class named Person, use the __init__() function to assign values for name and age:
#
class Person:
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
p1 = Person("Alex", 36, 400000)
print(p1.name)
print(p1.age)
print(p1.salary)
# Object Methods
# Objects can also contain methods. Methods in objects are functions that belongs to the object.
# Let us create a method in the Person class:
class Employee:
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Employee("John", 36, 500000)
p1.myfunc()
# The self Parameter
# The self parameter is a reference to the class itself, and is used to access variables that belongs to the class.
# It does not have to be named self ,
# you can call it whatever you like, but it has to be the first parameter of any function in the class:
class Person:
def __init__(mysillyobject, name, age, salary):
mysillyobject.name = name
mysillyobject.age = age
mysillyobject.salary = salary
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36, 456000)
p1.myfunc()
# Modify Object Properties
# You can modify properties on objects like this:
p1.age = 40
print(p1.age)
# Delete Object Properties
# You can delete properties on objects by using the del keyword:
del p1.age
print(p1.age)