polymorphism

Inheritance with relations and unique constraintss

Inheritance with relations and unique constraintss Question: I’m trying to achieve polymorphism as described in SQLAlchemy docs. I don’t really mind single table or joined table inheritance. My constraints are Child classes have references to direct parent classes, and only direct parent classes, not any class in the hierarchy: child -> parent, grand-child -> child. …

Total answers: 1

how to refer to a parent method in python?

how to refer to a parent method in python? Question: Suppose I have two classes (one a parent and one a subclass). How do I refer to a method in the parent class if the method is also defined in the subclass different? Here is the code: class A: def __init__(self, num): self.value=num def f(self, …

Total answers: 7

The inheritance of attributes using __init__

The inheritance of attributes using __init__ Question: I’m Java person who just started learning Python. Take this example: class Person(): def __init__(self, name, phone): self.name = name self.phone = phone class Teenager(Person): def __init__(self, name, phone, website): self.name=name self.phone=phone self.website=website I’m sure there’s a lot of redundant code (I know in Java, there are a …

Total answers: 6

Practical example of Polymorphism

Practical example of Polymorphism Question: Can anyone please give me a real life, practical example of Polymorphism? My professor tells me the same old story I have heard always about the + operator. a+b = c and 2+2 = 4, so this is polymorphism. I really can’t associate myself with such a definition, since I …

Total answers: 4

How does polymorphism work in Python?

How does polymorphism work in Python? Question: I’m new to Python… and coming from a mostly Java background, if that accounts for anything. I’m trying to understand polymorphism in Python. Maybe the problem is that I’m expecting the concepts I already know to project into Python. But I put together the following test code: class …

Total answers: 4

Re-raise exception with a different type and message, preserving existing information

Re-raise exception with a different type and message, preserving existing information Question: I’m writing a module and want to have a unified exception hierarchy for the exceptions that it can raise (e.g. inheriting from a FooError abstract class for all the foo module’s specific exceptions). This allows users of the module to catch those particular …

Total answers: 7

python properties and inheritance

python properties and inheritance Question: I have a base class with a property which (the get method) I want to overwrite in the subclass. My first thought was something like: class Foo(object): def _get_age(self): return 11 age = property(_get_age) class Bar(Foo): def _get_age(self): return 44 This does not work (subclass bar.age returns 11). I found …

Total answers: 11