Fork me on GitHub
  1. Python lazy hasattr()

    Python hasattr() evaluates the specified attribute, which may not be desired !

    class Attr(object):
        def __get__(self, obs, cls=None):
            print "evaluated"
            return 0
    
    class ClassA(object):
        a = Attr()
    
        @property
        def b(self):
            print "evaluated"
            return 0
    
    >>> c = ClassA()
    >>> c.a
    evaluated
    0
    >>> c.b
    evaluated
    0
    

    Now note that …

    read more

    There are comments.

« Page 2 / 2

social