inheritance - unexpected references to same object in different instances (python) -


some code have problem with:

# connector class conn():     def __init__(self, name):         self.name = name  # device base class class imper_dev():     names = []     conns = {}     def __init__(self):         name in self.names:             self.conns[name] = conn(name)  # real device           class dev(imper_dev):        names = ['a']         # instances of                d1 = dev()               d2 = dev()                >>> d1.conns['a'] <__main__.conn object @ 0x7fd68871c630>       >>> d2.conns['a']  <__main__.conn object @ 0x7fd68871c630> 

i little surprised behavior. intention create instances of dev() independent connectors. why have both instances d1 , d2 same reference conns['a']?

conns class variable means variable shared between instances of dev class. note d1 , d2 different objects, it's conns , names shared here.

>>> d1 <__main__.dev instance @ 0x7fb757329050> >>> d2 <__main__.dev instance @ 0x7fb7573290e0> 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -