简体   繁体   English

从类中访问类属性

[英]Accessing class properties from within a class

I am making an fantasy programming language for a game, and so I am writing the language and interpreter in python. 我正在为游戏制作幻想编程语言,所以我正在用python编写语言和解释器。 The language works as a stream of a commands, each one is interpreted each "step." 该语言就像命令流一样,每个“步骤”都被解释。 A string of code is inputted, and converted to a list of command objects. 输入一串代码,并将其转换为命令对象列表。 The interpreter handles the commands, but some of the commands need to be able to access attributes of the interpreter. 解释器处理命令,但是某些命令需要能够访问解释器的属性。

For example, the interpreter keeps track of the active command (numbered from the first command) so a goto command would need to change the active command. 例如,解释器会跟踪活动命令(从第一个命令开始编号),因此goto命令将需要更改活动命令。 The current way I do this is pass the data into a command, it is modified, and the interpreter sets it's attribute equal to it. 我当前的操作方式是将数据传递到命令中,对其进行修改,然后解释器将其属性设置为与之相等。

self.current = active.evaluate(self.current)

This sets the current command number equal to the active command evaluated with the current command number inputted. 这会将当前命令号设置为等于使用输入的当前命令号评估的活动命令。 This is how the whole block looks: 这是整个块的外观:

        if active.complete():  # on command completion
            type = active.return_type  # get return type
            # passes in requested data to modify
            if type == "var":  # changing var stream
                self.var = active.evaluate(self.var)
            elif type == "stream":  # changing packet stream
                self.stream = active.evaluate(self.stream)
            elif type == "current":  # changing current packet
                self.current = active.evaluate(self.current)
            elif type == "value":  # returns argument for next command
                self.holder = active.evaluate(self.var)
                self.resolve = True
            elif type == "none":
                active.evaluate()

This seems like a inconvient way of doing things, is there a better way of modifying class attributes from inside of class? 这似乎是一种不方便的处理方式,是否有更好的方法从类内部修改类属性?

My simple answer would be to simply pass in the interpreter itself and let the object change the relevant property on the interpreter. 我的简单答案是简单地传入解释器本身,然后让对象更改解释器上的相关属性。 If you didn't want to break too much from the format you already have, you could give the evaluate() function two arguments: 如果不想过多破坏已有的格式,可以给evaluate()函数提供两个参数:

def evaluate(interpreter, field_name):
    interpreter.setattr(field_name, some_value)

which you would then call like 然后您会称其为

active.evaluate(self, "var")

However, this seems like a good place to implement polymorphism, which would solve both the problem you're asking about and eliminate the need for a growing stack of if statements. 但是,这似乎是实现多态的好地方,它可以解决您所要解决的问题,并且不需要不断增长的if语句堆栈。

First, let's say you have a superclass for commands: 首先,假设您有一个命令超类:

class Command:
    def complete():
        # do something, return a boolean

    def evaluate(interpreter):
        raise NotImplementedException()

Currently, it seems like you're using the command's return_type to change what you're doing with the command. 当前,似乎您正在使用命令的return_type更改对命令的操作。 But why not simply detect what type of command is going to be run, and instantiate a different subclass with different behavior accordingly? 但是为什么不简单地检测将要运行的命令类型,并相应地实例化具有不同行为的另一个子类呢?

class GotoCommand(Command):
    def evaluate(interpreter):
        interpreter.current = some_value

class PacketCommand(Command):
    def evaluate(interpreter):
        interpreter.stream = some_other_value

...

after which you could just call active.evaluate(self) in the interpreter, without even having to care what kind of command it is. 之后,您可以在解释器中调用active.evaluate(self) ,而无需关心它是哪种命令。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM