Home  >  Article  >  Backend Development  >  How to call class methods in python

How to call class methods in python

尚
Original
2019-07-03 14:19:5317394browse

How to call class methods in python

Call of class method:

Similar to ordinary function call

1. Internal call of class: self.

2. Call outside the class: . (parameter list).

Note: In the above two calling methods, self does not need to be included in the parameter list provided.

Demonstrate a class:

wash.py
class Washer:
 
    def __init__(self):
        self.water = 0
        self.scour = 0
 
    def add_water(self,water):
        print('Add water:',water)
        self.water = water
 
    def add_scour(self,scour):
        self.scour = scour
        print('Add scour:',self.scour)
 
    def start_wash(self):
        print('Start wash...')
 
if __name__ == '__main__':
    w = Washer()
    w.add_water(10)
    w.add_scour(2)
    w.start_wash()

Running results:

How to call class methods in python

## For more Python related technical articles, please visit

Learn in the Python tutorial column!

The above is the detailed content of How to call class methods in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn