简体   繁体   中英

How to call a function from another class in python?

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!

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.

Define your function as bellow.

def getUsername(self):

def getAddress(self):

self is missing in your code.

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)

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()

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM