Python custom class and usage method
This article mainly introduces and uses python custom classes, and involves class definition and usage skills in Python. For more information, see
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
Class Person:
Def _ init _ (self, first, middle, last, age ):
Self. first = first;
Self. middle = middle;
Self. last = last;
Self. age = age;
Def _ str _ (self ):
Return self. first + ''+ self. middle +'' + self. last + \
''+ Str (self. age)
Def initials (self ):
Return self. first [0] + self. middle [0] + self. last [0]
Def changeAge (self, val ):
Self. age + = val
MyPerson = Person ('raja ',' I ', 'kumar', 21)
Print (myPerson)
MyPerson. changeAge (5)
Print (myPerson)
Print (myPerson. initials ())
|
The running result is as follows:
?
|
1
2
3 |
Raja I Kumar 21
Raja I Kumar 26
RIK
|
I hope this article will help you with Python programming.