Skip to content
Advertisement

Tag: oop

Terminology: A user-defined function object attribute?

According to Python 2.7.12 documentation, User-defined methods: User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object, an unbound user-defined method object, or a class method object. When the attribute is a user-defined method object, a new method object is only created

Python circular imports with inheritance

I have a parent and child class, where a parent’s method returns an instance of the child. Both classes are in separate files classA.py and classB.py. In order to avoid circular imports when I import classA I added the classB import to the end of classA.py (as shown below). Everything worked well and I was able to properly use classA

Override a “private” method in Python

Consider a class with a “private” method such as: When I try to subclass Foo and override method __method, it can be seen that Foo.__method is still called, instead of MoreFoo.__method. What would be the way to override such a method? Answer The point of using the double-underscore name convention is to prevent subclasses from (accidentally) overriding the method. If

Using the __call__ method of a metaclass instead of __new__?

When discussing metaclasses, the docs state: You can of course also override other class methods (or add new methods); for example defining a custom __call__() method in the metaclass allows custom behavior when the class is called, e.g. not always creating a new instance. [Editor’s note: This was removed from the docs in 3.3. It’s here in 3.2: Customizing class

Is there a benefit to defining a class inside another class in Python?

What I’m talking about here are nested classes. Essentially, I have two classes that I’m modeling. A DownloadManager class and a DownloadThread class. The obvious OOP concept here is composition. However, composition doesn’t necessarily mean nesting, right? I have code that looks something like this: But now I’m wondering if there’s a situation where nesting would be better. Something like:

Advertisement