这是我想要的例子:>>> 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'非常感谢您提供有关如何解决此问题的帮助.更新:我也尝试过这种方式:>>> test = [1, "test"]>>> mdc = MulticastDataClient()>>> mdc.add(test)>>> mdc.receive(){'192.168.1.10_1': 'test'}>>> test[1] = "change!">>> mdc.receive(){'192.168.1.10_1': 'change!'}这确实有效.但是,>>> val = "ftw!">>> nextTest = [4, val]>>> mdc.add(nextTest)>>> mdc.receive(){'192.168.1.10_1': 'change!', '192.168.1.10_4': 'ftw!'}>>> val = "different.">>> mdc.receive(){'192.168.1.10_1': 'change!', '192.168.1.10_4': 'ftw!'}这不起作用.我需要"ftw!"变得与众不同".在这种情况下.我正在使用字符串进行测试,并习惯于将字符串作为其他语言的对象.我将只编辑对象内部的内容,这样最终可以正常工作吗?解决方案在python中,所有内容都是引用,但字符串是不可变的.因此,test持有对"test"的引用.如果将应更改"分配给test,则只需将其更改为另一个引用即可.但是您的客户仍然引用测试".或更短一点:在python中无法正常工作! ;-)一种解决方案可能是将数据放入对象:data = {'someKey':"test"}mdc.add(data)现在,您的客户拥有对该词典的引用.如果您像这样更新字典,您的客户将看到更改:data['someKey'] = "this should change"I have a multicasting network that needs to continuously send data to all other users. This data will be changing constantly so I do not want the programmer to have to deal with the sending of packets to users. Because of this, I am trying to find out how I can make a reference to any object or variable in Python (I am new to Python) so it can be modified by the user and changes what is sent in the multicasting packets.Here is an example of what I want:>>> 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'Any help on how I can fix this would be very much appreciated.UPDATE:I have tried it this way as well:>>> test = [1, "test"]>>> mdc = MulticastDataClient()>>> mdc.add(test)>>> mdc.receive(){'192.168.1.10_1': 'test'}>>> test[1] = "change!">>> mdc.receive(){'192.168.1.10_1': 'change!'}This did work. However,>>> val = "ftw!">>> nextTest = [4, val]>>> mdc.add(nextTest)>>> mdc.receive(){'192.168.1.10_1': 'change!', '192.168.1.10_4': 'ftw!'}>>> val = "different.">>> mdc.receive(){'192.168.1.10_1': 'change!', '192.168.1.10_4': 'ftw!'}This does not work. I need 'ftw!' to become 'different.' in this case.I am using strings for testing and am used to strings being objects from other languages. I will only be editing the contents inside of an object so would this end up working? 解决方案 In python everything is a reference, but strings are not mutable. So test is holding a reference to "test". If you assign "this should change" to test you just change it to another reference. But your clients still have the reference to "test". Or shorter: It does not work that way in python! ;-)A solution might be to put the data into an object:data = {'someKey':"test"}mdc.add(data)Now your clients hold a reference to the dictionary. If you update the dictionary like this, your clients will see the changes:data['someKey'] = "this should change" 这篇关于Python参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 04:36