How to call methods in a class in python

(*-*)浩
Release: 2019-10-31 13:41:08
Original
7317 people have browsed it

How to call methods in a class in python

Calling methods in the same class

First of all, the method in the class needs to add the parameter self## when defining it. #, for example: (Recommended learning: Python video tutorial)

def SaveData(self,ip):
      print(ip)
Copy after login

If there is no self parameter, it cannot be called in the same class (I have made mistakes here before, be careful) If there is no self parameter, it can be called in another file, but it is not recommended. The standard writing method is that the first parameter of each function should be self (other names are also possible, but we all agree that the common name is written as self) ,

The following is an example:

class A():
    if __name__ == "__main__":
        print ('入口')
        self.Bfunc("192.168.1.1")

    def __init__(self):
        # 初始化;类似于C#中构造函数

    def Afunc(self,ip):
        print(ip)

    def Bfunc(self,ip):
        self.Afunc(ip)
Copy after login

Call a method in another class

A.py and B.py are in the same folder ,

can call each other through from file name import * or import A.

from A import A
a = A()
a.Afunc("123456")
a.Bfunc("123456")
Copy after login

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

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template