2

When a attribute/method (instance.something) is not found we can work around it by implementing a __getattr__ method. My question here is, how can we work around if a class method is not found.

class foo(object):
    @classmethod
    def bar(cls,a):
        print a

foo.bar("123")

# How to make below line work without throwing =  AttributeError: type object 'foo' has no attribute 'bar1'
foo.bar1("234")

Real time example :
@goncalopp : Basically we have a master library(say foo) which imports various other classes/lib and what not, in one very specific "weird" whatever situation, I should not import that master library so for few methods I need to implement a hack and make below line work with minimal code. Eg, self.foo.bar.Comment("print this comment") and self.foo.bar.Log("Print this log") and like that we have INFO, DEBUG, ERROR...
My take is :

foo = None
class Dummyfoo(object):
    class foo(object):  
        class bar(object):

            @classmethod
            def Comment(cls,a):
                print a

if foo == None:
    self = Dummyfoo()
    self.foo.bar.Comment("print this comment")  

So one way of achieving this is implement each of INFO, DEBUG... or one stop class method which will print whatever it is fed with.

Hope now the question and its purpose is more meaningful.

Thanks.

ramd
  • 347
  • 2
  • 12
  • are you trying to set *a specific attribute* of the class to some existing function, or do you want code called on *any* attribute access? If it's the last - *why*? What are your trying to do? – loopbackbee Sep 15 '14 at 10:36
  • Maybe using [http://stackoverflow.com/questions/11559120/getattr-on-a-class-and-not-or-as-well-as-an-instance?rq=1](metaclass)? – fredtantini Sep 15 '14 at 10:36

0 Answers0