This gives you the possibility to give each Countable subclass its own counter attribute when it is created: class CountableMeta(type):Ĭls. So what's the solution to this problem? You need a metaclass. # Does A have its own counter attribute now? You can see this in action here: class Countable: Instead of using a constructor method, use the standard pydantic syntax of declaring expected instance attributes on the child class. It's essentially equivalent to writing cls.counter = cls._base_.counter + 1. 1 Answer Sorted by: 0 It looks like BaseMemory from langchain is defined as a pydantic model, which has strict rules for defining instance attributes. For example, you could build a class representing a 3D shape by inheriting from two 2D shapes: class RightPyramid(Triangle, Square): def init(self, base, slantheight): self.base base self. adds a counter attribute to the child class that hasn't existed until now. A class can inherit from multiple parents. Then that value is incremented, and cls.counter =. Python allows you to inherit from two different classes by specifying them between parenthesis in the class declaration. In cls.counter + 1, cls doesn't have its own counter attribute yet, so this actually gives you the parent class's counter. However, it's important to understand what cls.counter refers to. This is equivalent to cls.counter = cls.counter + 1. If A had its own counter attribute, everything would work as expected: class Countable:īut if all of these classes share the same counter, why do we see different numbers in the output? That's because you actually add the counter attribute to the child class later, with this code: cls.counter += 1 Pass # A does not have its own counter, it shares Countable's counter They're merely inheriting it from Countable, so when Countable's counter changes, it looks like the child class's counter changes as well. The problem with your code is that subclasses of Countable don't have their own counter attribute. How come when B() is not initialized yet, it points to the same variable as A.counter but after creating single object it is a different one? Now, let's have some subclasses of this: class A(Countable): Where count(cls) is there for debugging purposes, and later i write it down. Python treats the first argument of a class method specially. Note that the first argument of each method is self. Return object._new_(cls, *args, increment_counter(cls): We define four class methods: init, increment, clear, getvalue. The new derived class is called the child class. Here is my implementation: class Countable(object): Inheritance in Python can be used to inherit the properties of the existing class to our new class. I want this counter to be increased also when a subclass (or subsubclass) of the given class is initialized. I was building a Countable class, which has a counter attribute that counts how many instances of the class have been initialized. Trying to understand oop in python I came into this situation that puzzles me, and I wasn't able to find a satisfactory explanation.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |