代码之家  ›  专栏  ›  技术社区  ›  X-VIPRIN

在Python中从实例调用函数时,为什么参数“self”需要使用字符串或字符作为参数?

  •  1
  • X-VIPRIN  · 技术社区  · 3 年前

    (我是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#
    
    

    正如你所看到的,我正在从一个名为“getBoundsForApplicationUse.py”的文件(下面该类的代码)创建一个类“getBoundsForApplicationUse”的实例,该实例名为“GetBoundsForApplication Use_”(请注意,实例名后面的underscoe“_”,类和实例名不相同)。从这一点开始,我调用函数“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
    

    我必须传递“”或任何文本字符串,否则会出现以下错误: “追踪(最近一次通话): 文件“E:\X-Viprin\GameCreation\AllProjects\Projects\Python\ViprinIRC\main.py”,第12行,在 getBoundsForApplicationUse_.welcomeUser()#欢迎用户使用IOStream中的应用程序# TypeError:getBoundsForApplicationUse.welcomeUser()缺少1个必需的位置参数:“self”

    我知道关键字“self”是安静的,类似于java中的关键字“this”。我可以使用“self”来引用这个类和其他类的类变量和方法。例如,我可以创建一个构造函数,并将3个不同的整数参数传递给该构造函数。从那里,我可以使用“self”关键字,将接收的参数设置为类变量: “self.myClassVariableOne=myReciveedArgumentOne”。 我对self关键字有一些想法,但为什么我需要向self关键字传递一个字符串,或者其他事情正在进行,而我没有意识到这一点?我查阅了一些教程,并将错误输入到搜索引擎中,但很难找到任何解决方案。

    有人能解释一下这里发生了什么吗?如果可以的话,那就谢谢你。

    ....................

    1 回复  |  直到 3 年前
        1
  •  0
  •   luigig44    3 年前

    您的问题在于:

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

    这不会创建类的实例,它只是创建对类本身的另一个引用。

    你应该这样做

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

    一旦你做到了, welcomeUser() 将自动接收您的实例作为 self 参数

    请记住,从来没有必要在代码中传递字符串。尝试传递5或数组。它也会起作用!这是因为 welcomeUser() 非常简单,只需要通过 某物 像 自己 但是 自己 通常应为类的实例。