[英]Saving the name of a instance of a class in a string variable
I have this strange question that im stuck for days. 我有一个奇怪的问题,我坚持了好几天。 I'm developing a small program using python for maya.
我正在使用python for maya开发一个小程序。 (By now I hope you know I'm not a professional programmer. just a side coder to build some internal scripts and tools)
(现在,我希望您知道我不是一个专业的程序员。只是用来构建一些内部脚本和工具的辅助编码器)
The tool I am building has a lot of UI elements I am creating as classes, which have their own methods. 我正在构建的工具具有许多我作为类创建的UI元素,它们具有自己的方法。
What I need to do, is to pass an instance of such a class, using a string. 我需要做的是使用字符串传递此类的实例。 I know this is strange and pls let me explain.
我知道这很奇怪,请允许我解释一下。
I need to implement a drag and drop operation of a widget class, which can pass a message from the source to the destination. 我需要实现小部件类的拖放操作,该操作可以将消息从源传递到目标。 the message always gets forcefully (by Maya) gets converted into a string!!!
消息总是被(玛雅人)强行转换成字符串!!!
Class a(object):
def __init__(self, v)
self.v=v
instance=a("important")
normally what I can do is 通常我能做的是
print (instance.v)
but what happens is during the message exchange process, the instance gets converted to text and I get a string instead of the python object 但是发生的是在消息交换过程中,实例被转换为文本,并且我得到了一个字符串而不是python对象
'< main .a object at 0x000001BB0696CA90>'
'< 主 .a位于0x000001BB0696CA90的对象>'
which is a string,. 这是一个字符串。 and i can't run or query any methods in the actual function..
而且我无法运行或查询实际函数中的任何方法。
I know this is very noob. 我知道这是非常菜鸟。 Please advice on how I can convert a class instance saved in a string to a python object so I can call up its methods or query variables..
请提供有关如何将保存在字符串中的类实例转换为python对象的建议,以便我可以调用其方法或查询变量。
Thanks again. 再次感谢。
How about writing a method for your class which returns the string? 如何为您的类编写一个返回字符串的方法?
class A(object):
def __init__(self, v):
self.v = v
def getV(self):
return self.v
Result: 结果:
>>> Instance = A('important')
>>> print(Instance.getV())
important
>>> Instance.getV()
'important'
If that doesn't work maybe you could write a printV()
method... 如果那不起作用,也许您可以编写一个
printV()
方法。
Well.. pickeling worked!! 好..酸洗工作!! i just wanted a way of creating a string from an instance and using the string to make the reference to the instance again.!!
我只是想从实例创建一个字符串,然后使用该字符串再次引用该实例。 thanks, @kiran Baktha!
谢谢@kiran Baktha!
i just did 我已经做了
beforetransfer = pickel.dumps(instance)
and at the recieving end 在接收端
aftertransfer = pickel.loads(beforetransfer)
thanks community!! 谢谢社区! you guys are amazzing!!
你们很赞!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.