overriding

Force child class to override parent's methods

Force child class to override parent's methods Question: Suppose I have a base class with unimplemented methods as follows: class Polygon(): def __init__(self): pass def perimeter(self): pass def area(self): pass Now, let’s say one of my colleagues uses the Polygon class to create a subclass as follows: import math class Circle(Polygon): def __init__(self, radius): self.radius …

Total answers: 3

How to 'update' or 'overwrite' a python list

How to 'update' or 'overwrite' a python list Question: aList = [123, ‘xyz’, ‘zara’, ‘abc’] aList.append(2014) print aList which produces o/p [123, ‘xyz’, ‘zara’, ‘abc’, 2014] What should be done to overwrite/update this list. I want the o/p to be [2014, ‘xyz’, ‘zara’, ‘abc’] Asked By: pyLearner || Source Answers: You may try this alist[0] …

Total answers: 6

Is it possible in Python to declare that method must be overridden?

Is it possible in Python to declare that method must be overridden? Question: I have an “abstract” class, such as: class A: def do_some_cool_stuff(): ”’ To override ”’ pass def do_some_boring_stuff(): return 2 + 2 And class B, subclassing A: class B(A): def do_stuff() return 4 Is there any way to declare, that a method …

Total answers: 1

How to detect method overloading in subclasses in python?

How to detect method overloading in subclasses in python? Question: I have a class that is a super-class to many other classes. I would like to know (in the __init__() of my super-class) if the subclass has overridden a specific method. I tried to accomplish this with a class method, but the results were wrong: …

Total answers: 10

Override the {…} notation so i get an OrderedDict() instead of a dict()?

Override the {…} notation so i get an OrderedDict() instead of a dict()? Question: Update: dicts retaining insertion order is guaranteed for Python 3.7+ I want to use a .py file like a config file. So using the {…} notation I can create a dictionary using strings as keys but the definition order is lost …

Total answers: 7

Overriding properties in python

Overriding properties in python Question: So, I’m trying to figure out the best (most elegant with the least amount of code) way to allow overriding specific functions of a property (e.g., just the getter, just the setter, etc.) in python. I’m a fan of the following way of doing properties, due to the fact that …

Total answers: 3

Python: multiplication override

Python: multiplication override Question: So, I’ve got a custom class that has a __mul__ function which works with ints. However, in my program (in libraries), it’s getting called the other way around, i.e., 2 * x where x is of my class. Is there a way I can have it use my __mul__ function for …

Total answers: 2

Creating a namedtuple with a custom hash function

Creating a namedtuple with a custom hash function Question: Say I have a namedtuple like this: FooTuple = namedtuple(“FooTuple”, “item1, item2”) And I want the following function to be used for hashing: foo_hash(self): return hash(self.item1) * (self.item2) I want this because I want the order of item1 and item2 to be irrelevant (I will do …

Total answers: 3

How do I override a Python import?

How do I override a Python import? Question: I’m working on pypreprocessor which is a preprocessor that takes c-style directives and I’ve been able to make it work like a traditional preprocessor (it’s self-consuming and executes postprocessed code on-the-fly) except that it breaks library imports. The problem is: The preprocessor runs through the file, processes …

Total answers: 3

How to prevent a function from being overridden in python

How to prevent a function from being overridden in Python Question: Is there a way to make a class function unoverriddable? Something like Java’s final keyword. I.e, any overriding class cannot override that method. Asked By: olamundo || Source Answers: The issue is you are trying to write in Python using Java philosophies. Some thing …

Total answers: 4