Python中的动态对象命名和类调用

2024-09-28 05:27:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python开发一种编程语言,在这里你可以编写简单机器的模拟程序。我已经编写了一个函数,它接受一些输入,解析它,并找出第一个单词是什么。你知道吗

现在,对于第一个单词insert,我需要使用下一个单词objnamexy。你知道吗

obj: what type of simple machine it is
name: what you want to call the object
x: X coordinate on the graph
y: Y coordinate on the graph

我已经创建了一个函数nextword,它遍历代码的其余部分,并将每个变量定义为这些单词,因此使用以下代码:

insert pulley JohnThePulley 3 4

它看到第一个单词是insert,并调用我的insert函数。
然后,它将obj设置为pulleyname设置为JohnThePulley,依此类推。你知道吗

但是,现在我需要在子类pulley中,在母类simple_machines下创建一个名为JohnThePulley等的对象

我所处的情况是,例如,对于第一个单词insert,我完全不知道下一个单词是什么,从他们可以调用的所有子类的选择中。我需要创建指定的对象以及提供的名称、提供的X坐标和提供的Y坐标。你知道吗

我尝试过用'{}'.format(name).format(obj)在python中进行简单的格式化,但这些都不起作用。你知道吗

# Insert function
def insert(code):
    c = 4
    syntax = np.array([obj, name, x, y])
    nextword(parser.code_array, syntax, c)
    objc += 1
    return


# Nextword function, code_array[0] is insert, syntax is an array that
# contains all the variables that need to be defined for any function
def nextword(code_array, syntax, c):
    assert len(code_array) == c + 1, "Too Many Words!"
    for m in range(0, c):
        syntax[m] = code_array[m + 1]
    return


# Mother Class simple_machines with properties
class simple_machines:
    def __init__(self, obj, name, x, y, coords):
        self.obj = (
            obj
        )  # what type of obj, in this case, pulley
        self.name = name  # name, JohnThePulley
        self.x = x  # 3 in this case
        self.y = y  # 4 in this case
        self.coords = (x, y)  # (3,4) in this case
        return


# Pulley Class, here so I can later define special properties for a pulley
class pulley(simple_machines):
    def __init__(self, name, x, y):
        super(simple_machines, self).__init__()
        return


# Code that I tried
def insert(code):
    c = 4
    syntax = np.array([obj, name, x, y])
    nextword(parser.code_array, syntax, c)
    "{}".format(name) = "{}".format(obj)(
        name, x, y
    )  # this is what my
    # instantiation would look like, formatting an object with name, then
    # calling a class formatted with obj, and inserting their input of
    # name,x,y as the properties
    return

我希望在pulley中创建一个名为JohnThePulley,坐标为X=3和Y=4的对象。简单地说,我想要得到的是一个名为name的对象,它位于一个名为obj的类中,具有name.xname.y等属性

但是,我会遇到如下错误:

NameError: name 'obj' is not defined

或:

SyntaxError: can't assign to function call

第一个显然意味着单词obj没有被赋值,但是第二个显然意味着我不能格式化函数名或变量名并将其定义为函数(即使我将其实例化为类)。你知道吗

我做错什么了?我怎样才能解决这个问题?你知道吗


Tags: the函数nameselfobjiscodesimple
1条回答
网友
1楼 · 发布于 2024-09-28 05:27:13

name 'obj' is not defined是因为obj是在另一个函数中定义的。您必须使用MYOBJECT.obj,而不是单独使用obj,还必须保留对MYOBJECT的引用。你知道吗

'{}'.format(obj)(name,x,y)没有任何意义,'{}'.format(obj)是一个字符串,不可调用。你知道吗

SyntaxError: can't assign to function call是您似乎感兴趣的实际问题。你可以做globals()['{}'.format(name)] = stuff,但它不适用于局部变量和对象(你的linter也不会喜欢它)。你知道吗

如果要对对象执行相同的操作,可以使用setattr(MYOBJECT, '{}'.format(name), '{}'.format(obj))

以上所有的解决方案在技术上都被认为是“丑陋的”,而您可能正在寻找的是一个字典,虽然它不是面向对象的,但字典在幕后被用来准确地处理您想要对对象执行的操作。没有方法的对象本质上是一个公正的字典。你知道吗

mydico = dict()
mydico[name] = obj

另外,如果name是字符串,那么'{}'.format(name)等价于name。你知道吗

相关问题 更多 >

    热门问题