[英]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.