Skip to content
Advertisement

Tag: inheritance

How to print an instance of a parent class in base class?

I am able to print the name of Class Student when I inherit the class into Sports and define the values in the init method. but I am not able to figure out how to print the name given when Student class is created without defining the name in Sports class, When I comment out the Student.__init__(self, 1, “name1”, “addr1”,

Init super with existing instance?

Suppose I have: How do I correctly initialize the super class with the output of the super class method rather than init? My OOP background is in C++ and I am continually getting into these scenarios due to the ability to overload constructors in C++, so a workaround for this would be awesome. Answer @shx2’s answer works but wastefully/awkwardly creates

What are “inheritable alternative constructors”?

I stumbled over the term “inheritable alternative constructors” in this answer: https://stackoverflow.com/a/1669524/633961 The link points to a place where classmethod gets explained. Do other programming languages have this feature, too? Answer One of the things that you can do with ANY language that has class methods (or similar) is provide alternative constructors. A slightly contrived Python3 example below : with

subclassing dict; dict.update returns incorrrect value – python bug?

I needed to make a class that extended dict and ran into an interesting problem illustrated by the dumb example in the image below. Why is d.update() ignoring the class’s __getitem__? EDIT: This is in python2.7 which does not appear to contain collections.UserDict Thinking UserDict.UserDict is the equivalent I tried this, and it gets closer, but still behaves interestingly. Answer

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

How to create a subclass in python that is inherited from turtle Module

So, i’m trying to learn python and every time i post a question here it feels like giving in… I’m trying to make my own class of turtle.Turtle. Gives the Traceback: AttributeError: ‘TurtleGTX’ object has no attribute ‘_position’. Which I then learn is a “private vairable” which according to the offical python tutorial i can mangle/override in my subclass TurtleGTX.

Advertisement