Search code examples
pythonsetattr

python object member variable missing


I have a constructor which creates a member object of a sub-class:

class OpenSprinkler:
  class CV:
    my_args = ['rsn', 'rbt', 'en', 'rd', 're']
    my_longhand = {'reset_all':'rsn',
                   'reboot':'rbt',
                   'enable':'en',
                   'rain_delay':'rd',
                   'remote_extension':'re'}

    def __init__(self, p):
      print('set parent of %r\n' % (self,))
      self.parent = p
      print('parent set to %r\n' % (p,))
      self.my_args.extend(self.my_longhand.keys())

  def __init__(self, hostname, password, log=None):
    self.cv = self.CV(self)

the output confirm that the constructor for the member was called:

>>> c = openSprinkler.OpenSprinkler('foo','bar')
set parent of <openSprinkler.OpenSprinkler.CV object at 0xb6a58df0>

parent set to <openSprinkler.OpenSprinkler object at 0xb6a58dd0>

>>> repr(c.cv)
'<openSprinkler.OpenSprinkler.CV object at 0xb6a58df0>'

but when I look at the members, cv.parent does not appear to exist:

>>> dir(c.cv)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'my_args', 'my_longhand']

cv.my_args contains the extended list. Any idea what's going on here?

UPDATE: As Alec pointed out, the code example actually does work. The actual problem had been omitted from the example and was that I had chosen to override the setattr method without allowing parent to be set.

originally:

def __setattr__(self, name, value):
  if name in self.my_args:
    if name in self.my_longhand.keys():
      name = self.my_longhand[name]
    self.parent._json_get('cv', {name:value})

now:

def __setattr__(self, name, value):
  if name in self.my_args:
    if name in self.my_longhand.keys():
      name = self.my_longhand[name]
    self.parent._json_get('cv', {name:value})
  else:
    super().__setattr__(name, value)

Solution

  • Is it possible that you have an openSprinkler object somewhere else in the code that's being called? Instantiating OpenSprinkler directly seems to contain your methods:

    c = OpenSprinkler('foo','bar')
    
    print(dir(c.cv))
    # ['__class__', '__delattr__', '__dict__', '__dir__', 
    # '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
    # '__gt__', '__hash__', '__init__', '__le__', '__lt__', 
    # '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
    # '__repr__', '__setattr__', '__sizeof__', '__str__', 
    # '__subclasshook__', '__weakref__', 'my_args', 'my_longhand', 'parent']