How to call a function from another class in python?

南楼画角 提交于 2019-12-08 01:41:22

问题


I'm very new to python and Sikuli software. :( I was trying to call a function from another class. Here is a class which contains the functions.

class Reader():
    def getUsername():
        ....
        return username

    def getAddress():
        .....
        return address

Here is another class which calls the functions stated above.

class DisplayInfo():
    reader = Reader()
    uName = reader.getUsername()
    addr = reader.getAddress()
    ......
    ......

But when I ran DisplayInfo class, I got this error.

uName = reader.getUsername()
TypeError: getUsername() takes no arguments (1 given)

Could anyone help me solve this? Thanks so much in advance!


回答1:


When defining Reader, the functions need to be defined to take a self argument, which is the instance of the class they were called on:

class Reader():
    def getUsername(self):
        ....
        return username

    def getAddress(self):
        .....
        return address

The error you're getting is because Python is trying to pass the class instance to the functions, but since they're defined as taking no arguments, it fails to pass that one argument.




回答2:


Python implicitly passes the object to method calls, but you need to explicitly declare the parameter for it. This is customarily named self:

def getUserName(self)
def getAdress(self)



回答3:


Define your function as bellow.

def getUsername(self):

def getAddress(self):

self is missing in your code.




回答4:


Try to this.

class Reader():
def getUsername(self):
    username = "name"
    return username

def getAddress(self):
    address = 'address'
    return address


class DisplayInfo():
    reader = Reader()
    uName = reader.getUsername()
    addr = reader.getAddress()

reader = Reader()
uName = reader.getUsername()
uAddress = reader.getAddress()



回答5:


Python always implicitly passes the object to the function, that's why you get the error. You solve this by changing it to

getUsername(self):
...
return username

Classes are used for this very reason, so that you encapsulate the functions and variables into objects that are only available to themselves. In order for a completely different object to have access to stuff inherent to something else, there's three ways:

Instantiation

Basically what you just did, you instantiated a reader object and called it's method.

Class in a class

If one of the classes is a child class, the child can use the parents methods.

Inheritance

You make the class inherit the properties of the other class. It's very powerful and fun once you get the hang of it.




回答6:


Each function in python module needs to have first argument as self, This is not having any specific meaning but if you don’t follow the convention you will definitely land into the error state.

class Reader():
    def getUsername(self):
        ....

    def getAddress(self):
        .....

You can refer following doc for more info Python Classes



来源:https://stackoverflow.com/questions/30432564/how-to-call-a-function-from-another-class-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!