Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dir(element) returns non-existing element. Trying to use getattr(element, ...) fails

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?

like image 483
RedX Avatar asked Jan 21 '26 15:01

RedX


1 Answers

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

like image 193
abrunet Avatar answered Jan 24 '26 04:01

abrunet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!