Changing variables in multiple Python instances

后端 未结 4 429
感情败类
感情败类 2021-01-18 22:24

Is there anyway to set the variables of all instances of a class at the same time? I\'ve got a simplified example below:

class Object():
   def __init__(self         


        
4条回答
  •  隐瞒了意图╮
    2021-01-18 22:37

    You could use a class attribute:

    class Object():
       speed = 0 
    
    instance0=Object()
    instance1=Object()
    instance2=Object()
    Object.speed=5
    # instance0.speed == instance1.speed == instance2.speed == Object.speed == 5
    

    However this would mean that all instances would always have the same speed.

提交回复
热议问题