3

Basically what i do is:

attrs = dir(oe)
for attr in attrs:
  attr_obj = getattr(oe, attr)

  .... more code ...

but the getattr call fails with: AttributeError: no such child: comment

oe is an ObjectifiedElement of the lxml.objectify library.

When i look into oe with PyCharm it shows the comment attribute but also fails to resolve it.

What is going on here? How can this attribute be shown by dir when it does not exist?

RedX
  • 14,749
  • 1
  • 53
  • 76
  • I do not see the `comment` attribute in my lxml version's `ObjectifiedElement` . Can you show an [MCVE] that reproduces your issue. – Anand S Kumar Oct 27 '15 at 09:43
  • @AnandSKumar No i cannot, as the code that does this is merging various xml files together and i guess it some point a comment gets inserted and then removed. – RedX Oct 27 '15 at 09:49

1 Answers1

4

I'm no expert but lxml maybe redefining __getattr__ . From their source code:

def __getattr__(self, tag):
    u"""Return the (first) child with the given tag name.  If no namespace
    is provided, the child will be looked up in the same one as self.
    """
    if is_special_method(tag):
        return object.__getattr__(self, tag)
    return _lookupChildOrRaise(self, tag)

see https://github.com/lxml/lxml/blob/master/src/lxml/lxml.objectify.pyx

Moreover, about the dir method, you have:

dir([object]) Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.

If the object has a method named dir(), this method will be called and must return the list of attributes. This allows objects that implement a custom getattr() or getattribute() function to customize the way dir() reports their attributes.

If the object does not provide dir(), the function tries its best to gather information from the object’s dict attribute, if defined, and from its type object. The resulting list is not necessarily complete, and may be inaccurate when the object has a custom getattr().

see https://docs.python.org/2/library/functions.html#dir

abrunet
  • 1,122
  • 17
  • 31
  • Yeah i guess this is the root of the problem. `ObjectifiedElement` overrides `__dict__` with a different method then it uses to look up things in `__getattr__`. – RedX Oct 27 '15 at 09:51