动态对象命名和 Class 调用 Python
Dynamic Object Naming and Class Calling in Python
我正在 Python 开发一种编程语言,您可以在其中编写简单机器的模拟程序。我编写了一个函数,它接受一些输入,对其进行解析,然后找出第一个单词是什么。
现在,对于插入的第一个单词,我需要输入接下来的单词 obj
、name
、x
和 y
。
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
设置为 pulley
,将 name
设置为 JohnThePulley
,依此类推。
但是,现在我需要在母亲 class simple_machines
下的女儿 class pulley
中创建一个对象,其名称为 JohnThePulley
,等等
我现在的情况是,对于插入的第一个单词,我根本不知道下一个单词是什么,从女儿的所有选择中,classes可以打电话。我需要创建指定的对象以及提供的名称、提供的 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
的 class 中,具有属性 name.x
、name.y
等
但是,我收到如下错误:
NameError: name 'obj' is not defined
或:
SyntaxError: can't assign to function call
第一个显然意味着单词 obj
没有被分配,但第二个显然意味着我无法格式化函数名称或格式化变量名称并将其定义为函数(即使我将其实例化为 class)。
我做错了什么?我该如何解决这个问题?
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))
以上所有解决方案均采用技术术语 "ugly",您可能正在寻找的是字典,虽然它不是 OOP,但在幕后使用字典来准确处理您的问题想用对象来做。没有方法的对象本质上只是一个字典。
mydico = dict()
mydico[name] = obj
另外,如果 name
是一个字符串,那么 '{}'.format(name)
等价于 name
.
我正在 Python 开发一种编程语言,您可以在其中编写简单机器的模拟程序。我编写了一个函数,它接受一些输入,对其进行解析,然后找出第一个单词是什么。
现在,对于插入的第一个单词,我需要输入接下来的单词 obj
、name
、x
和 y
。
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
设置为 pulley
,将 name
设置为 JohnThePulley
,依此类推。
但是,现在我需要在母亲 class simple_machines
下的女儿 class pulley
中创建一个对象,其名称为 JohnThePulley
,等等
我现在的情况是,对于插入的第一个单词,我根本不知道下一个单词是什么,从女儿的所有选择中,classes可以打电话。我需要创建指定的对象以及提供的名称、提供的 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
的 class 中,具有属性 name.x
、name.y
等
但是,我收到如下错误:
NameError: name 'obj' is not defined
或:
SyntaxError: can't assign to function call
第一个显然意味着单词 obj
没有被分配,但第二个显然意味着我无法格式化函数名称或格式化变量名称并将其定义为函数(即使我将其实例化为 class)。
我做错了什么?我该如何解决这个问题?
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))
以上所有解决方案均采用技术术语 "ugly",您可能正在寻找的是字典,虽然它不是 OOP,但在幕后使用字典来准确处理您的问题想用对象来做。没有方法的对象本质上只是一个字典。
mydico = dict()
mydico[name] = obj
另外,如果 name
是一个字符串,那么 '{}'.format(name)
等价于 name
.