Questions tagged [subclassing]

A subclass, "derived class", heir class, or child class is a modular, derivative class that inherits one or more language entities from one or more other classes.

A subclass, "derived class", heir class, or child class is a modular, derivative class that inherits one or more language entities from one or more other classes (called superclasses, base classes, or parent classes). The semantics of class inheritance vary from language to language, but commonly the subclass automatically inherits the instance variables and member functions of its superclasses. Some languages support the inheritance of other construct as well. For example, in Eiffel, contracts which define the specification of a class are also inherited by heirs. The superclass establishes a common interface and foundational functionality, which specialized subclasses can inherit, modify, and supplement. The software inherited by a subclass is considered reused in the subclass. A reference to an instance of a class may actually be referring one of its subclasses. The actual class of the object being referenced is impossible to predict at compile-time. A uniform interface is used to invoke the member functions of objects of a number of different classes. Subclass may replace superclass functions with entirely new functions that must share the same method signature.

833 questions
9
votes
1 answer

Calling __new__ when making a subclass of tuple

In Python, when subclassing tuple, the __new__ function is called with self as an argument. For example, here is a paraphrased version of PySpark's Row class: class Row(tuple): def __new__(self, args): return tuple.__new__(self,…
Paul
  • 3,321
  • 1
  • 33
  • 42
9
votes
2 answers

Python decorators and class inheritance

I'm trying to use decorators in order to manage the way users may or may not access resources within a web application (running on Google App Engine). Please note that I'm not allowing users to log in with their Google accounts, so setting specific…
jbmusso
  • 3,436
  • 1
  • 25
  • 36
9
votes
1 answer

Subclassing core Ruby class such as Hash

I need a class that acts like a Hash, though not necessarily with all the Hash methods. I've read that it is not a good idea to subclass core classes like Hash. Whether or not that is true, what is the best practice for doing this kind of thing? #…
Mike Blyth
  • 4,158
  • 4
  • 30
  • 41
8
votes
2 answers

Defining __repr__ when subclassing set in Python

I'm trying to subclass the set object in Python, using code similar to the below, but I can't work out a sensible definition of __repr__ to use. class Alpha(set): def __init__(self, name, s=()): super(Alpha, self).__init__(s) …
me_and
  • 15,158
  • 7
  • 59
  • 96
8
votes
2 answers

Django: extend get_object for class-based views

Being a non-expert Python programmer, I'm looking for feedback on the way I extended the get_object method of Django's SingleObjectMixin class. For most of my Detail views, the lookup with a pk or slugfield is fine - but in some cases, I need to…
Gregor
  • 95
  • 2
  • 4
8
votes
3 answers

Returning subclass object from superclass method

I keep coming back to variants of this problem: it probably has a very simple solution, but I can't seem to figure it out... I have a bunch of classes of the form xQuantity, e.g. DistanceQuantity, AreaQuantity, etc., which extend a class…
Paul Morrison
  • 1,694
  • 3
  • 20
  • 35
8
votes
3 answers

Subclassing UINavigationBar ... how do I use it in UINavigationController?

I wanted to subclass UINavigationBar (to set a custom background image & text color) and use that for all the navigation bars in my app. Looking at the API docs for UINavigationController, it looks like navigationBar is…
8
votes
1 answer

Python subclassing a class with custom __new__

There's a class (not created by me, from a 3rd party library) that has no __init__ declared (other than object's __init__), and this is its __new__: def __new__(cls, uid): self = super().__new__(cls, uid) self._info = get_info_from_uid(uid) …
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
8
votes
3 answers

Custom UIToolBar from Images

I need to create a UIToolbar object that uses an image for the background. Most of the buttons are images as well, and rectangular. One button, however, is round and overlaps the toolbar like the Start button on the Windows task bar. See below. I…
Sophtware
  • 1,796
  • 2
  • 21
  • 34
8
votes
1 answer

How to subclass Navigation Controller when using storyboards?

I'm using storyboards in interface builder using the Xcode menu 'Editor...Embed in...Navigation Controller'. It seems that in iOS 6 you have to subclass the UINavigationController to allow all orientations, with -…
7
votes
1 answer

unexpected keyword argument 'sample_weight' when sub-classing tensor-flow loss class (categorical_crossentropy) to created a weighted loss function

Struggling to get a sub-classed loss function to work in Tensorflow (2.2.0). Initially tried this code (which I know has worked for others - see https://github.com/keras-team/keras/issues/2115#issuecomment-530762739): import tensorflow.keras.backend…
Philip Alton
  • 131
  • 1
  • 6
7
votes
2 answers

Subclassing models in Rails

I have two models, Article and Recipe, which have a bunch of the same attributes and methods. I want to make the subclasses of a new class "Post" and move all their shared logic in there so I'm not maintaining duplicate code. I've tried…
weotch
  • 5,788
  • 6
  • 35
  • 42
7
votes
1 answer

Subclassing multiprocessing.managers.BaseProxy

I'm having some trouble trying to implement a new defaultdict proxy object. The documentation is a bit scares, so I'm not sure how to go about this correctly. I want to add a defaultdict to the list of types that are available from the Manager…
freebie
  • 2,161
  • 2
  • 19
  • 36
7
votes
3 answers

How to init my subclass with an instance of its superclass?

In my app I read calendar events of type EKEvent, and I've made an extension with a lot of computed vars so I can easily get the duration, number of man-hours etc. for each event in the calendar. But in large scale, the performance is bad - so I…
Esben von Buchwald
  • 2,772
  • 1
  • 29
  • 37
7
votes
1 answer

SetWindowSubclass changes ANSI windows to UNICODE

Is SetWindowSubClass() supposed to change an ANSI window into a UNICODE widow? I didn't find anything in the documentation, or on the web, about this behavior. I created a test application (full source) just to illustrate how SetWindowSubclass (I…