Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What method does Python call when I access an attribute of a class via the class name?

I know accessing the attributes of Foo through an instance will call the __getattribute__() method, but what if I access this attribute directly through the class? If a function is called, I want to set a breakpoint in it so that the breakpoint can be triggered when accessing this property through a class in my project.

I have tried to set breakpoint in magic method __getattribute__(), but nothing hapened.

class Foo:
    age = 18


print(Foo.age)  # I am curious what method is called
like image 725
fitz Avatar asked Jul 17 '20 05:07

fitz


People also ask

How do you access the attributes of a class in Python?

Accessing the attributes of a class getattr() − A python method used to access the attribute of a class. hasattr() − A python method used to verify the presence of an attribute in a class. setattr() − A python method used to set an additional attribute in a class.

How do you call an attribute in Python?

Attributes of a class can also be accessed using the following built-in methods and functions : getattr() – This function is used to access the attribute of object. hasattr() – This function is used to check if an attribute exist or not. setattr() – This function is used to set an attribute.

What are attributes and methods in a Python class?

A variable stored in an instance or class is called an attribute. A function stored in an instance or class is called a method.

When class method is called in Python?

To call a class method, put the class as the first argument. Class methods can be can be called from instances and from the class itself. All of these use the same method. The method can use the classes variables and methods.

Why do we use the class method in Python?

Therefore the class method gives us control of changing the class state. You may use a variable named differently for cls, but it is discouraged since self is the recommended convention in Python. The class method can only access the class attributes, not the instance attributes

How do I define a class attribute in Python?

To define a class attribute, you place it outside of the __init__ () method. Use class_name.class_attribute or object_name.class_attribute to access the value of the class_attribute.

What are attribute and methods in Python?

Accessing Attributes and Methods in Python Last Updated : 23 Nov, 2020 Attributes of a class are function objects that define corresponding methods of its instances. They are used to implement access controls of the classes.

How do Python class attribute namespaces handle assignment?

The instance namespace takes supremacy over the class namespace: if there is an attribute with the same name in both, the instance namespace will be checked first and its value returned. Here’s a simplified version of the code ( source) for attribute lookup: With this in mind, we can make sense of how Python class attributes handle assignment:


1 Answers

Everything in Python is an object and everything has a type that determines the object's behavior. This also holds for class objects. You can check type(Foo):

>>> type(Foo)
<class 'type'>

You can customize the type of a class by providing a custom metaclass. This metaclass is then responsible for attribute access on the class object:

class MyMeta(type):
    def __getattribute__(self, name):
        print(f'__getattribute__({self!r}, {name!r})')
        return super().__getattribute__(name)


class Foo(metaclass=MyMeta):
    age = 18


print(Foo.age)

and the output is:

__getattribute__(<class '__main__.Foo'>, 'age')
18
like image 60
a_guest Avatar answered Nov 05 '22 04:11

a_guest