在Python中,当从一个示例调用一个函数时,为什么参数'self'需要接受一个字符串或char作为参数?

ryoqjall  于 2023-06-28  发布在  Python
关注(0)|答案(1)|浏览(88)

(我是Python新手,总体上不太擅长OOP编程)
我以前编写过Python程序(比如6到7个测试功能的小程序),但我总是呆在一个文件中。在一个Python文件中创建一个类,在同一个文件中创建一个示例,并调用类作用域之外的函数。我从来不需要向任何只有“self”作为参数的函数传递任何参数。但现在我在这里有这个类:main.py:

#"main.py" is the main script of the application; The application executes this script first;#

#IMPORT FORMAT: from FILENAME import CLASSNAME
from getBoundsForApplicationUse import getBoundsForApplicationUse; #Class is part of application.#

#Variables#
connectOrCreateServer = 2 #Will be updated with users choice; 0 = choose to host/create server; 1 = choose to connect as a client to an existing server;#

#Entry Point#
getBoundsForApplicationUse_ = getBoundsForApplicationUse; #Create instance of class "getBoundsForApplicationUse"; Check top of that specific class for information;#
getBoundsForApplicationUse_.welcomeUser(''); #Welcome the user to the application in IOStream#

正如你所看到的,我从一个名为“www.example.com”的文件中创建了一个类“getBoundsForApplicationUse”的示例getBoundsForApplicationUse.py(下面是该类的代码),该示例名为“getBoundsForApplicationUse_”(注意示例名称后面的下划线“_”,类和示例名称不相同)。从这一点开始,我调用函数“welcomeUser()”,它只有'self'作为参数:getBoundsForApplicationUse.py:

#"getBoundsForApplicationUse.py", will query the user, getting information on server or client connection for the IRC to connect; I.e, this script will get IP Address, Port Number, ask to start a server, ask to start a client, etc.#

class getBoundsForApplicationUse:

    #This function allows the user to choose to connect to a server, or host one; Should return value back to variable "connectOrCreateServer" in "main.py"; 0 = choose to host/create server; 1 = choose to connect as a client to an existing server;#
    def userChooseOption(self):

        return

    #This function welcomes the user to the application; Message created in IOStream#
    def welcomeUser(self):
        print("Welcome to ViprinIRC! A lightweight IRC chat!\n")
        return

我必须传递''或任何文本字符串,否则我会得到这个错误:“Traceback(最后一次调用):文件“E:\X-Viprin\GameCreation\AllProjects\Projects\Python\ViprinIRC\main.py”,第12行,在getBoundsForApplicationUse_.welcomeUser()中;#在IOStream中欢迎用户进入应用# TypeError:getBoundsForApplicationUse.welcomeUser()缺少1个必需的位置参数:“自我”
我知道关键字'self'和java中的关键字'this'很相似。我可以使用'self'来引用这个类和其他类的类变量和方法。例如,我可以创建一个构造函数,我可以向该构造函数传递3个不同的整数参数。从那里,我可以使用'self'关键字,将接收到的参数设置为类变量:“self.myClassVariableOne = myRecievedArgumentOne”。我对self关键字有一些概念,但是为什么我需要向self关键字传递一个字符串,或者是其他一些东西,我没有意识到?我查找了一些教程,并将错误输入到搜索引擎中,很难找到任何解决方案。
有人能解释一下这里发生了什么吗?如果你能,那么谢谢你。
....................

cgfeq70w

cgfeq70w1#

你的问题在这里:

getBoundsForApplicationUse_ = getBoundsForApplicationUse; #Create instance of class "getBoundsForApplicationUse";

这不会创建类的示例,它只是创建了对类本身的另一个引用。
你应该做的

getBoundsForApplicationUse_ = getBoundsForApplicationUse(); #Create instance of class "getBoundsForApplicationUse"
getBoundsForApplicationUse_.welcomeUser()

完成此操作后,welcomeUser()将自动接收您的示例作为self参数。
请记住,在代码中从来没有必要传递字符串。尝试传递5或数组。它也会工作!这是因为welcomeUser()非常简单,只需要你传递东西作为self,但self通常被期望是类的示例。

相关问题