我有一个多播网络,需要不断向所有其他用户发送数据.这些数据会不断变化,所以我不希望程序员必须处理向用户发送数据包.因此,我试图找出如何在Python中引用任何对象或变量(我是Python新手),因此可以由用户修改它并更改多播数据包中发送的内容.
这是我想要的一个例子:
>>> test = "test"
>>> mdc = MulticastDataClient()
>>> mdc.add(test) # added into an internal list that is sent to all users
# here we can see that we are successfully receiving the data
>>> print mdc.receive()
{'192.168.1.10_0': 'test'}
# now we try to change the value of test
>>> test = "this should change"
>>> print mdc.receive()
{'192.168.1.10_0': 'test'} # want 'test' to change to -> 'this should change'
Run Code Online (Sandbox Code Playgroud)
任何有关我如何解决这个问题的帮助将非常感激.
更新:
我也尝试过这种方式:
>>> test = [1, "test"] …Run Code Online (Sandbox Code Playgroud) 我需要能够将对象的任何字符串表示形式转换回另一台计算机上的原始状态.我将使用类A,作为我的例子:
class A:
def __init__(self):
self.data = "test"
self.name = "Bob"
def __str__(self):
return str(self.data) + " " + str(self.name)
Run Code Online (Sandbox Code Playgroud)
必须能够在单独的计算机上重新创建A类对象.因此,如果收到一台单独的计算机("A","memberData memberName"),它可以将其转换为类A的对象.我需要使用任何用户定义的对象.我希望用户只需要创建一个toString()和toObject()方法.