如何在课程外使用方法? [英] How do I use a method outside a class?

查看:27
本文介绍了如何在课程外使用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习python类.我在我们的论坛上要求提供有关此方面的提示,但是没有运气.我认为我的实施非常糟糕.我很新,所以即使我问这个问题的方式也要忍受.

I'm taking python classes. I've asked for hints about this in our forums but with no luck. I think my implementation is very bad. I'm very new at this, so bear with me, even with the way I ask the question.

上面的问题是告诉我我需要做的事情.我尝试了但是却没有运气,所以我来这里寻求帮助.

The question above is what I am told I need to do. I've tried but with no luck, so I've come here for help.

最终,我试图让我的按键处理程序响应我的按键操作.之前我已经做过,但是我们还没有开始上课.那就是障碍所在.我应该实现类方法/变量以使其工作,而不要使用新的变量或新的全局变量.

Ultimately, I am trying to get my key handlers to respond to my keypresses. I've done this previously, but we were not yet working with classes. That's where the snag is. I'm supposed to implement class methods/variables to make them work, not use new variables or new globals.

例如

class SuchAndSuch:

    def __init__(self, pos, vel, ang, ang_vel, image, info, sound = None):
        self.pos = [pos[0],pos[1]]
        self.vel = [vel[0],vel[1]]
        self.angle = ang
        self.angle_vel = ang_vel
        self.image = image

    def update(self):
        # this is where all the actual movement and rotation should happen
        ...

下面的处理程序在SuchAndSuch类之外:

The handler below is outside the SuchAndSuch class:

def keydown(key):
    # need up left down right buttons
    if key == simplegui.KEY_MAP["up"]:
        # i'm supposed to just call methods here to make the keys respond???

    ...

因此,所有更新都应该在SuchAndSuch类中进行,并且对此更新的 only 调用应该位于我的密钥处理程序内.

So, all updates are supposed to be happening in the SuchAndSuch class and only calls for this updates are supposed to be inside my keyhandler.

有人可以给我一个例子,说明他们说这话的意思吗?我尝试在密钥处理程序中将所有变量(或论坛中给出的想法)错误定义为未定义".

Can someone please give me an example on what they mean when they say this? All the variables (or ideas given in forums) I try to implement in my key handlers error as "undefined".

推荐答案

有两种方法可以从该类外部调用该类的方法.更常见的方法是在类的实例上调用该方法,如下所示:

There are two ways to call a class' methods from outside that class. The more common way is to call the method on an instance of the class, like this:

# pass all the variables that __init__ requires to create a new instance
such_and_such = SuchAndSuch(pos, vel, ang, ang_vel, image, info)

# now call the method!
such_and_such.update()

就这么简单!方法定义中的 self 参数是指调用该方法的实例,并作为第一个参数隐式传递给该方法.您可能希望 such_and_such 成为模块级(全局")对象,以便每次按下键时都可以引用和更新同一对象.

Simple as that! The self parameter in the method definition refers to the instance that the method is being called on, and is implicitly passed to the method as the first argument. You probably want such_and_such to be a module-level ("global") object, so you can reference and update the same object every time a key is pressed.

# Initialize the object with some default values (I'm guessing here)
such_and_such = SuchAndSuch((0, 0), (0, 0), 0, 0, None, '')

# Define keydown to make use of the such_and_such object
def keydown(key):
    if key == simplegui.KEY_MAP['up']:
        such_and_such.update()
        # (Perhaps your update method should take another argument?)


第二种方法是调用 class方法.这可能不是您在此处实际想要的,但是为了完整起见,我将对其进行简要定义:将类方法绑定到 class 上,而不是该类的实例.您使用装饰器声明它们,因此您的方法定义如下所示:


The second way is to call a class method. This is probably not what you actually want here, but for completeness, I'll define it briefly: a class method is bound to a class, instead of an instance of that class. You declare them using a decorator, so your method definition would look like this:

class SuchAndSuch(object):
    @classmethod
    def update(cls):
        pass # do stuff

然后您可以在没有类实例的情况下调用此方法:

Then you could call this method without an instance of the class:

SuchAndSuch.update()

这篇关于如何在课程外使用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆