E
Python has variable and non-replaceable types. The variables are that their contents can be changed without changing the reference to them. Intangible objects have to be redesigned to reflect changes in status. However, all the old references don't see this update because they're pointing to an old object.I'll explain in practice. Lists, dictionaries, many are variable objects:l1 = [1, 2, 3]
l2 = l1
print(l1, l2, id(l1), id(l2))
# [1, 2, 3] [1, 2, 3] 139917408901064 139917408901064
l1[1] = 10
print(l1, l2, id(l1), id(l2))
[1, 10, 3] [1, 10, 3] 139917408901064 139917408901064
Chisla, rows, arts are intact objects:v1 = 1024
v2 = v1
print(v1, v2, id(v1), id(v2))
1024 1024 ...7040 ...7040
v1 = 2048
print(v1, v2, id(v1), id(v2))
2048 1024 ...5312 ...7040
t1 = (1, 2, 3)
t2 = t1
print(t1, t2, id(t1), id(t2))
(1, 2, 3) (1, 2, 3) ...6232 ...6232
t1[1] = 10 # не сработает, так как кортежи неизменяемые
t1 = (1, 10, 3)
print(t1, t2, id(t1), id(t2))
(1, 10, 3) (1, 2, 3) ...7240 ...6232
In your code, you use variable data structures such as dictionaries and lists.user♪ userdata♪ server) and intangible lines and numbers.Thus, without altering the structure of the storage of information, only access to the list of numbers can be reduced as much as possiblemy_userdata = server[serveraddr][username]
изменение my_userdata влечёт изменение server[serveraddr][username],
так как это один и тот же объект
As the number of non-replaceable, the continuation of these variables and the further change of this variable do not affect the reference number (i.e. the variable) joincount Never change with change server[serveraddr][username][0]and vice versa. The change of numbers on this list will have no impact on the variables stored these values.But if you try to improve the code a little bit, it's worth creating a class. UserDatawhich will keep the necessary values and allow them to change. The minimum example will be:class UserData:
def init(self, joincount, leftcount, gamescount, wincount):
self.joincount = joincount
self.leftcount = leftcount
self.gamescount = gamescount
self.wincount = wincount
username = "test"
userdata = UserData(10, 2, 30, 15)
user = {username:userdata}
After that, if we get our user from somewhere, we can change the necessary values:user_data = server[serveraddr][username]
user_data.joincount = 20
user_data.leftcount = 9
user_data.gamescount = 10
user_data.wincount = 3
print (server[serveraddr][username].joincount,
server[serveraddr][username].leftcount,
server[serveraddr][username].gamescount,
server[serveraddr][username].wincount)
print (user_data.joincount, user_data.leftcount,
user_data.gamescount, user_data.wincount)
The use of such a structure would increase the readability of your code compared with the use of the list indices.If there's access to a change in the contents of the dictionary, I'd recommend that you use the self-written class instead of using it. namedtuple standard module collectionswhich allows for the creation of an object similar to a cortic, but which provides access to fields as in the class above (but only for reading):UserData = collections.namedtuple('UserData',
['joincount', 'leftcount', 'gamescount', 'wincount'])
In this case, we'll have to. Replace old object UserData new:old_userdata = server[serveraddr][username]
new_userdata = UserData(joincount=20, leftcount=9, gamescount=10, wincount=3)
server[serveraddr][username] = new_userdata
print(new_userdata, old_userdata, server[serveraddr][username])
Receipt field will be performed as in the example above:print(old_userdata.joincount, old_userdata.gamescount)