Adding `__getattr__` method to an existing object instance

nathanlct

I would like this to work:

import types

def new_getattr(self, *args, **kwargs):
    return 2

class A:
    def __init__(self):
        pass

a = A()
a.__getattr__ = types.MethodType(new_getattr, a)
print(a.anything)

Right now, it throws AttributeError: A instance has no attribute 'anything'.

I tried different solutions proposed here and they work, but not for __getattr__.

If I do print(a.__getattr__('anything')), it actually prints 2; the problem is that my __getattr__ method is not called automatically when I do a.anything.


As a side note, in my actual implementation, I cannot modify the definition of the class A, nor can I type its name and do something like A.__getattr__ = ... (which would work) because I need this to be generic and independent of the class name.

Edit: I ended up doing it like this:

a.__class__.__getattr__ = new_getattr.

wim

You can not - __dunder__ names are resolved on the type, not per-instance. Custom __getattr__ will need to be defined directly on A.

See Special method lookup section of the datamodel documentation, specifically:

For custom classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary.

Note: if you only have a reference to an instance, not the class, it is still possible to monkeypatch the type by assigning a method onto the object returned by type(a). Be warned that this will affect all existing instances, not just the a instance.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Cheking if an object has an attribute, without relying on '__getattr__'

分類Dev

Firebase set method not adding data to an existing Collection

分類Dev

Use of __getattr__ in Python

分類Dev

object .__ getattr__が欠落しているのはなぜですか?

分類Dev

Adding a row to an existing table

分類Dev

Is it possible to create object instance through method reference in Kotlin

分類Dev

How could I use an object's method calling the thread instance of it?

分類Dev

"TypeError: Invalid attempt to spread non-iterable instance": Adding either an Array or Object to an Array

分類Dev

Adding existing canvas to a div dynamically

分類Dev

Adding metadata to existing python objects

分類Dev

Find existing instance of Office Application

分類Dev

how to make classes with __getattr__ pickable

分類Dev

Pythonでの__getattr__の使用

分類Dev

adding a char in every instance of substring

分類Dev

Using Generic T of Method to create new instance of another object with the same Generic T

分類Dev

Call parent method in JavaScript class but stll have access to prototype methods inside object instance?

分類Dev

Swift - how do I pass a type/object (name or instance) into a method and then (a) get the type (b) instantiate it?

分類Dev

Adding a method call to a collection

分類Dev

Pythonの__getattr__メソッドを使用したオブジェクトのpickleは、 `TypeError、object notcallable`を返します

分類Dev

object .__ getattribute__は、記述子と__getattr__の__get__メソッドにどのようにリダイレクトしますか?

分類Dev

Overwrite method on Pydantic instance

分類Dev

Adding elements to an object in a loop

分類Dev

Adding object collision in pygame

分類Dev

Delphi AddObject -not adding object

分類Dev

Adding a Calendar object to a ParseObject

分類Dev

Matplotlib adding legend based on existing color series

分類Dev

Adding a new consortium definition to existing running network

分類Dev

Stripe - adding new customers to an existing plan?

分類Dev

Adding more properties to existing CQRS Events

Related 関連記事

  1. 1

    Cheking if an object has an attribute, without relying on '__getattr__'

  2. 2

    Firebase set method not adding data to an existing Collection

  3. 3

    Use of __getattr__ in Python

  4. 4

    object .__ getattr__が欠落しているのはなぜですか?

  5. 5

    Adding a row to an existing table

  6. 6

    Is it possible to create object instance through method reference in Kotlin

  7. 7

    How could I use an object's method calling the thread instance of it?

  8. 8

    "TypeError: Invalid attempt to spread non-iterable instance": Adding either an Array or Object to an Array

  9. 9

    Adding existing canvas to a div dynamically

  10. 10

    Adding metadata to existing python objects

  11. 11

    Find existing instance of Office Application

  12. 12

    how to make classes with __getattr__ pickable

  13. 13

    Pythonでの__getattr__の使用

  14. 14

    adding a char in every instance of substring

  15. 15

    Using Generic T of Method to create new instance of another object with the same Generic T

  16. 16

    Call parent method in JavaScript class but stll have access to prototype methods inside object instance?

  17. 17

    Swift - how do I pass a type/object (name or instance) into a method and then (a) get the type (b) instantiate it?

  18. 18

    Adding a method call to a collection

  19. 19

    Pythonの__getattr__メソッドを使用したオブジェクトのpickleは、 `TypeError、object notcallable`を返します

  20. 20

    object .__ getattribute__は、記述子と__getattr__の__get__メソッドにどのようにリダイレクトしますか?

  21. 21

    Overwrite method on Pydantic instance

  22. 22

    Adding elements to an object in a loop

  23. 23

    Adding object collision in pygame

  24. 24

    Delphi AddObject -not adding object

  25. 25

    Adding a Calendar object to a ParseObject

  26. 26

    Matplotlib adding legend based on existing color series

  27. 27

    Adding a new consortium definition to existing running network

  28. 28

    Stripe - adding new customers to an existing plan?

  29. 29

    Adding more properties to existing CQRS Events

ホットタグ

アーカイブ