简体   繁体   English

您可以将未解决的参数传递给 python 中的 class function 吗?

[英]can you pass an unresolved argument to a class function in python?

I tried to pass a parameter into a function inside a class, usually when I pass a parameter into a function I can use an unresolved attribute like so:我尝试将参数传递给 class 内的 function,通常当我将参数传递给 function 时,我可以使用如下未解析的属性:

    def noodle(spices):
        print(f"noodle with {spices})

and depending on which spices I choose to enter when I'm using the function, that spice will be used in the print function like so:并且取决于我在使用 function 时选择输入的香料,该香料将用于打印 function,如下所示:

    noodle(salt)
    noodle with salt

but when I try to do the same thing inside a class, pycharm tells me the argument "player" is unresolved and I can't seem to be able to do the exact same thing I otherwise would outside of the class, here is the code I am having trouble with:但是当我尝试在 class 中做同样的事情时,pycharm 告诉我参数“播放器”尚未解决,我似乎无法做与 ZA2F2ED4F8EBC40ABDZC21A 之外完全相同的事情,否则代码如下我遇到了以下问题:

    from turtle import Turtle



    class Paddle(Turtle):

        def __init__(self):
            super().__init__()
            self.player1 = Turtle('square')
            self.create_paddle(self.player1)

        def create_paddle(self, player):
            self.player.penup()
            self.player.color('white')
            self.player.shapesize(stretch_wid=1, stretch_len=0.5)

I want to be able to create multiple paddles and reuse that function, but my method doesn't seem to work and I don't understand why我希望能够创建多个桨并重用 function,但我的方法似乎不起作用,我不明白为什么

Thank you for the help, the solution was indeed very simple and it looks like that:感谢您的帮助,解决方案确实非常简单,看起来像这样:

from turtle import Turtle


class Paddle(Turtle):

    def __init__(self):
        super().__init__()
        self.player1 = Turtle('square')
        self.create_paddle(self.player1)

    def create_paddle(self, player):
        player.penup()
        player.color('white')
        player.shapesize(stretch_wid=2, stretch_len=0.5)

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

相关问题 您可以在Python中传递成为类实例的参数吗? - Can you pass an argument in Python that becomes a class instance? Python,如何将函数作为参数传递给类方法 - Python, How to pass function to a class method as argument 如何在Python类函数中隐式传递参数 - How to pass argument implicitly in Python Class function Python:如何忽略函数中的参数? - Python: How can you ignore an argument in a function? 我可以将异常作为参数传递给python中的函数吗? - Can I pass an exception as an argument to a function in python? 在 python 中创建一个将数组名称作为参数的函数。 我收到错误“未解析的引用”,然后是我尝试传递的参数 - Creating a function that takes an array name as an argument in python. I get the error “unresolved reference to” and then the argument i try to pass 你能把 arguments 传递给 ZA7F5F35426B927411FC9231B5638271 中的 function object 吗? - Can you pass arguments to a function object in Python? 您可以将变量传递给 python 的导入 function 吗? - Can you pass a variable to the import function of python? Python,你可以从一个类中将模块级函数传递给ThreadPoolExecutor - Python, can you pass a module level function to ThreadPoolExecutor from within a class 您可以在 python class 的 static 方法中使用 self 参数吗? - Can you use the self argument in a static method in python class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM