Cara menggunakan python __getattribute__

Python’s magic method __getattribute__() implements the built-in getattr() function that returns the value associated with a given attribute name. If the __getattribute__() error results in an

class Person:
    def __getattribute__(self, attr_name):
        print('hello world')
    

alice = Person()
getattr(alice, 'age')
# hello world
0 due to a non-existent attribute, Python will call the
class Person:
    def __getattribute__(self, attr_name):
        print('hello world')
    

alice = Person()
getattr(alice, 'age')
# hello world
1 function for resolution.

Thus, the __getattribute__() method takes precedence over the

class Person:
    def __getattribute__(self, attr_name):
        print('hello world')
    

alice = Person()
getattr(alice, 'age')
# hello world
1 method.

We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.

Syntax and Minimal Example

object.__getattribute__(self, attr_name)

Let’s have a look at an example where you override the

class Person:
    def __getattribute__(self, attr_name):
        print('hello world')
    

alice = Person()
getattr(alice, 'age')
# hello world
4 magic method of a custom class
class Person:
    def __getattribute__(self, attr_name):
        print('hello world')
    

alice = Person()
getattr(alice, 'age')
# hello world
5 to simply print out the string
class Person:
    def __getattribute__(self, attr_name):
        print('hello world')
    

alice = Person()
getattr(alice, 'age')
# hello world
6 when calling the getattr() built-in function.

class Person:
    def __getattribute__(self, attr_name):
        print('hello world')
    

alice = Person()
getattr(alice, 'age')
# hello world

__getattribute__ vs __getattr__

The

class Person:
    def __getattribute__(self, attr_name):
        print('hello world')
    

alice = Person()
getattr(alice, 'age')
# hello world
4 method is similar to
class Person:
    def __getattribute__(self, attr_name):
        print('hello world')
    

alice = Person()
getattr(alice, 'age')
# hello world
9. What’s the difference?

Assume a user wants to access an attribute from a given object like so:

my_obj.my_attr
  • my_obj.my_attr
    0 is called first. If it yields an
    class Person:
        def __getattribute__(self, attr_name):
            print('hello world')
        
    
    alice = Person()
    getattr(alice, 'age')
    # hello world
    
    0, Python will hand it to:
  • my_obj.my_attr
    2 which is called second.

You can see that the former takes precedence over the latter in the following code snippet that defines both methods—but __getattribute__() is taken as it doesn’t result in an error.

class Person:
    def __getattribute__(self, attr_name):
        print('hello world')

    def __getattr__(self, attr_name):
        print('hello universe')
    

alice = Person()
getattr(alice, 'age')
# hello world

Here’s what would’ve happened in the same scenario when raising an

class Person:
    def __getattribute__(self, attr_name):
        print('hello world')
    

alice = Person()
getattr(alice, 'age')
# hello world
0 in
class Person:
    def __getattribute__(self, attr_name):
        print('hello world')
    

alice = Person()
getattr(alice, 'age')
# hello world
4:

class Person:
    def __getattribute__(self, attr_name):
        raise AttributeError

    def __getattr__(self, attr_name):
        print('hello universe')
    

alice = Person()
getattr(alice, 'age')
# hello universe

Python doesn’t even mention the error but passes the execution flow to the

class Person:
    def __getattribute__(self, attr_name):
        print('hello world')
    

alice = Person()
getattr(alice, 'age')
# hello world
1 method.

Background getattr()

Python’s built-in

my_obj.my_attr
7 function returns the value of the
my_obj.my_attr
8‘s attribute with name
my_obj.my_attr
9.

If this doesn’t exist, it returns the value provided as an optional third

class Person:
    def __getattribute__(self, attr_name):
        print('hello world')

    def __getattr__(self, attr_name):
        print('hello universe')
    

alice = Person()
getattr(alice, 'age')
# hello world
0 argument.

If that doesn’t exist either, it raises an

class Person:
    def __getattribute__(self, attr_name):
        print('hello world')
    

alice = Person()
getattr(alice, 'age')
# hello world
0.

Python getattr() - Ultimate Guide

Cara menggunakan python __getattribute__

Watch this video on YouTube

An example is

class Person:
    def __getattribute__(self, attr_name):
        print('hello world')

    def __getattr__(self, attr_name):
        print('hello universe')
    

alice = Person()
getattr(alice, 'age')
# hello world
2 which is equivalent to
class Person:
    def __getattribute__(self, attr_name):
        print('hello world')

    def __getattr__(self, attr_name):
        print('hello universe')
    

alice = Person()
getattr(alice, 'age')
# hello world
3.

# Define class with one attribute
class Car:
    def __init__(self, brand, speed):
        self.brand = brand
        self.speed = speed


# Create object
porsche = Car('porsche', 100)
tesla = Car('tesla', 110)

# Two alternatives to get instance attributes:
print(getattr(porsche, 'brand') + " " + str(getattr(porsche, 'speed')))
print(tesla.brand + " " + str(tesla.speed))


# Get an attribute that doesn't exist with default argument:
print(getattr(porsche, 'color', 'red'))

Output:

porsche 100
tesla 110
red

Further Reading:

  • Python
    class Person:
        def __getattribute__(self, attr_name):
            print('hello world')
    
        def __getattr__(self, attr_name):
            print('hello universe')
        
    
    alice = Person()
    getattr(alice, 'age')
    # hello world
    
    4 magic method
  • Python
    class Person:
        def __getattribute__(self, attr_name):
            print('hello world')
    
        def __getattr__(self, attr_name):
            print('hello universe')
        
    
    alice = Person()
    getattr(alice, 'age')
    # hello world
    
    5 built-in function
  • Python
    class Person:
        def __getattribute__(self, attr_name):
            print('hello world')
    
        def __getattr__(self, attr_name):
            print('hello universe')
        
    
    alice = Person()
    getattr(alice, 'age')
    # hello world
    
    6 built-in function
  • Python
    class Person:
        def __getattribute__(self, attr_name):
            print('hello world')
        
    
    alice = Person()
    getattr(alice, 'age')
    # hello world
    
    1 vs __getattribute__()
  • https://docs.python.org/3/reference/datamodel.html

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Cara menggunakan python __getattribute__

Chris

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.