How to call class methods in python

Release: 2019-07-03 14:19:53
Original
17327 people have browsed it

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()
Copy after login

Running results:

How to call class methods in python

## For more Python related technical articles, please visit

Learn in the Python tutorialcolumn!

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!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!