python getattr from class method, why is it not finding the other class method?

spacether
class Dog(object):
    def __init__(self):
        self.type = 'corgi'
        self.barked = False
    def __bark(self):
        print('__woof')
        self.barked = True
    def _bark(self):
        print('_woof')
        self.barked = True
    def call_method(self, method_name):
        method = getattr(self, method_name)()
d = Dog()
d.call_method('_bark')  # this works
d.call_method('__bark')  # this raise an AttributeError

I have a dog class where I want to use getattr to dynamically find a method under self When I try to find the method name of a class with two underscores is fails but if I use one underscore it works. Why is the double underscore method not seen by getattr?

spacether

Double underscore methods are name mangled per here: What is the meaning of a single- and a double-underscore before an object name?

If you need to access your methods with getattr make them have no leading underscores, or one underscore.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Method from other class and use of toString()

Why is binding a class instance method different from binding a class method?

Why is binding a class instance method different from binding a class method?

Swift - Delegate not calling method from other class

Why can't I call a method from my Python class?

Breaking a while loop from other method inside the same class in Python

Call a static method from other class in python

Redirect print stream from other class method

Call class method from function running in other process in Python

Using method from one class within method for other class

Why is the NameError from the set method in my Python class not working?

Mockito uses method stubbing from other test method in the same class

Python: Import errors. Accessing method from other class

Python no output from class method

Issue starting method from other class

Calling a class method from another class method in Python 2.7

Calling method from one class in other

Android getting parameter from one method to another method in other class

Python: how to monkey patch class method to other class method

coffeescript class - calling calling one class method from an other

Class method points to other method of other class

How to get access the method from other method of class?

Python3 How to call to method from one class in other class

Access to class method from other class method

Python class calling other method

JS - Calling method in class from other method in same class

How to call method from other class

Calling a method from another method inside the same class using getattr

Python class call a method from a class object