Python Links

I have a multicast network that must constantly send data to all other users. This data will constantly change, so I do not want the programmer to deal with sending packets to users. Because of this, I am trying to figure out how I can refer to any object or variable in Python (I am new to Python), so it can be changed by the user and changes what is sent in multicast packages.

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 greatly appreciated.

UPDATE:

I also tried this:

 >>> 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!'} 

It really worked. Nonetheless,

 >>> 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!" become "different." in this case. I use strings for testing, and I use objects from other languages ​​for strings. I will only edit the contents inside the object, so will this end the job?

+6
source share
2 answers

In python, everything is referenced, but the lines are not changed. So, test holds a link to "test". If you assigned β€œthis should change” to test , you simply change it to another link. But your customers still have a link to the "test". Or in short: in python this does not work !; -)

The solution may be to put data in an object:

 data = {'someKey':"test"} mdc.add(data) 

Your customers now have a dictionary link. If you update the dictionary this way, your customers will see the changes:

 data['someKey'] = "this should change" 
+5
source

You cannot, not easily. The name (variable) in Python is just a place for a pointer. Overwrite it and you just replace the pointer with another pointer, i.e. The change will be visible only to those who use the same variable. The members of the object are basically the same, but since their status is visible to everyone with a pointer to them, you can propagate such changes. You just need to use obj.var every time. Of course, strings (along with integers, tuples, several other built-in types, and several other types) are immutable, i.e. You cannot change anything so that others can see, because you cannot change it at all.

However, the variability of objects opens up another possibility: you could, if you bother to pull it out, write a wrapper class containing an abritary object that allows you to change this object using the set() method and delegate everything that is important for this object . Sooner or later you might run into unpleasant troubles. For example, I cannot imagine that this would play well with metaprogramming that goes through all the participants or something that thinks it needs to be tinkered with. It is also incredibly hacked (i.e. Unreliable). Perhaps this is a lot easier.

(On the side of the note, PyPy has the become function in one of the unsafe objects that really and truly replace one object with another, visible to everyone with reference to this object. However, it does not work with any other implementations, and I think it's incredible the potential and misuse of confusion, as well as the fact that most of us rarely ever needed it, makes it almost unacceptable in real code.)

+3
source

Source: https://habr.com/ru/post/889480/


All Articles