FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PythonLearning/29.ClassWithMethods.py at Pythontuts · aditya0035/PythonLearning · GitHub
aditya0035
/
PythonLearning
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
PythonLearning
/
29.ClassWithMethods.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
36 lines (30 loc) · 1.29 KB
Breadcrumbs
PythonLearning
/
29.ClassWithMethods.py
Copy path
File metadata and controls
36 lines (30 loc) · 1.29 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""
In python class method are defined just like function
but it has an additional parameter which is first parameter
self represent current instance as it has to acces the properties from that object
These method are called instance method as they work on instance/object of the class
example
"""
class
Student
:
def
__init__
(
self
,
name
):
self
.
Name
=
name
self
.
Marks
=
[]
def
average
(
self
):
return
sum
(
self
.
Marks
)
/
len
(
self
.
Marks
)
jignesh
=
Student
(
"Jignesh"
)
jignesh
.
Marks
.
append
(
40
)
jignesh
.
Marks
.
append
(
80
)
jignesh
.
Marks
.
append
(
90
)
jignesh
.
Marks
.
append
(
70
)
jignesh
.
Marks
.
append
(
90
)
print
(
jignesh
.
average
())
#here it is equivelent to this
#the python will do like this
#ClassName.InstanceMethodName(Object,OtherArgs..)
print
(
Student
.
average
(
jignesh
))
#although we can give anyname to self but its a standard convenstion in python to use self to refer object instance
#Self is the first parameter of an instance method of a class
#here we write self.Name so these are property of instance/self so these will varies from object to objects
#we can also put the object inside a like below
my_friend_list
=
[
"Aditya"
,
"Ramesh"
,
"Ashish"
]
student_list
=
[
Student
(
name
)
for
name
in
my_friend_list
]
print
(
student_list
)
#here it is the list of student object
Back
|
FazBrowse Home
|
New Git URL