简体   繁体   English

如何从 class 外部调用 class 内部的 function?

[英]How do i call a function that's inside a class from outside of the class?

So i'm trying to access the function "saveuserdata" from outside the "Account" class in PyCharm.所以我试图从 PyCharm 中的“帐户”class 外部访问 function“saveuserdata”。 But it keeps saying Unresolved reference 'saveuserdata'但它一直说Unresolved reference 'saveuserdata'

class Account:
    def __init__(self, password, username):
        self.password = password
        self.username = username

    def login(self, x, y):
        if x == self.password and y == self.username:
            print("Correct, Logging on...")
        else:
            print("Incorrect username or password, please try again.")  

    def saveuserdata(self):
        with open("user_data.txt", "a+") as file_object:
            # Move read cursor to the start of file.
            file_object.seek(0)
            # If file is not empty then append '\n'
            data = file_object.read(100)
            if len(data) > 0:
                file_object.write("\n")
            # Append text at the end of file
            file_object.write("Username: " + self.username + " Password: " + self.password)`

Add these two lines outside class在 class 之外添加这两行

a = Account("username", "password")
a.saveuserdata()

The saveuserdata function needs to be called on an instance of the class.需要在saveuserdata的实例调用 saveuserdata function。

# Create instance
acc = Account("blah", "blah")

# Call function
acc.saveuserdata()

This is shorthand for:这是以下的简写:

acc = Account("blah", "blah")
Account.saveuserdata(acc)

Maybe you don't have any object that uses Account.也许您没有任何使用帐户的 object。 You should call the method on the object that inherits from your class, not the class itself.您应该调用从 class 继承的 object 上的方法,而不是 class 本身。 Need more info to know exactly what your problem is.需要更多信息才能确切知道您的问题是什么。

You have two options:你有两个选择:

  1. declare an object of class Account, and then call the method on the object,just like upwards.声明一个class账户的object,然后调用object上的方法,同上。
  2. declare @classmethod annotation on the class, then call the method on the class.在 class 上声明 @classmethod 注解,然后在 class 上调用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果函数(在类之外)需要使用类中的方法,我该如何调用该方法? - If a function (that's outside of the class) needs to use a method from a class, how do I call the method? 如何在 class 定义中调用 class function ? - How do I call a class function inside the class definition? 如何返回在类内部的函数中更改的变量,并在类外部的函数中使用它 - How Do I return a variable that is changed within a function inside a class and use it in a function outside of the class 您如何从类内部调用私有模块函数? - How do you call a private module function from inside a class? 如何在 FlaskForm class 内部的 validate() function 外部调用变量 - How to call a variable outside a validate() function which is inside the FlaskForm class 在任何方法之外调用函数但在类中? - Call a function outside any method but inside a class? tkinter,无法从类外部调用函数 - tkinter, unable to call a function from outside of it's class Python:如何从类中的函数调用特定变量? - Python: How do I call a specific variable from a function inside the class? 如何在 class 中调用 function? - How can I call a function inside a class? Python:如何从外部的 function 更改 class 的值 - Python: How to change the value of a function inside a class from an outside function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM