[英]define global variable in autobahn with python
我想用python autobahn創建一個聊天應用程序,將接收到的消息發送給所有客戶端。因此首先我必須定義一個全局變量(如字典)並在其上放置一些信息(client:消息尚未下載)。 我需要定義一個全局變量,但我做不到,我定義了像這樣的代碼的字典,但是它對於每個客戶端都是唯一的
class MyProtocol(WebSocketServerProtocol):
clients_msgs = {} # this must be global for all clients
.
.
.
那么我應該如何定義我的變量呢?
您必須在python中使用ClassName.attributeName作為所有類實例之間的全局變量。
>>> class Test(object):
... i = 3
...
>>> Test.i
3
>>> t = Test()
>>> t.i # static variable accessed via instance
3
>>> t.i = 5 # but if we assign to the instance ...
>>> Test.i # we have not changed the static variable
3
>>> t.i # we have overwritten Test.i on t by creating a new attribute t.i
5
>>> Test.i = 6 # to change the static variable we do it by assigning to the class
>>> t.i
5
>>> Test.i
6
>>> u = Test()
>>> u.i
6
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.