4,171 questions
0
votes
1
answer
38
views
Subclass of Generic fails with AttributeError: object has no attribute '__parameters__' - using Generic when superclass does forward __init_subclass__
I have a setup like the following
from typing import Generic, TypeVar
T = TypeVar("T")
class ThirdParty:
def __init_subclass__(cls):
... # does not call super()
class Mine(...
0
votes
0
answers
14
views
How can I subclass a JComobBox and handle events there but, have access to the top level class?
How can I move then event handling into the subclass of a JComboBox? I can make it work if everything is in TopLevel but, I want to move the event handling (actionPerformed(ActionEvent e)) into a ...
2
votes
1
answer
178
views
Is it possible to limit attributes in a Python sub class using __slots__?
One use of __slots__ in Python is to disallow new attributes:
class Thing:
__slots__ = 'a', 'b'
thing = Thing()
thing.c = 'hello' # error
However, this doesn’t work if a class inherits from ...
-2
votes
2
answers
93
views
How do I call the method from a class that creates other objects? [closed]
I have five classes. My Main class, a form class, two subclasses that extend the form class and a createForm class I am forced to use to create new instances of the subclass.
public class FormenFabrik ...
0
votes
0
answers
31
views
Constructor and datatype not matching in java [duplicate]
I'm relatively new to Java and object oriented programming, and I don't understand why sometimes the constructor used in declaring and object will be a subclass of the objects type instead of the ...
1
vote
2
answers
101
views
How do you extend a superclass property object in a subclass in JavaScript and get TypeScript to not complain?
Given this code:
class Foo {
data = { x: 1 };
}
class Bar extends Foo {
override data = { ...super.data, y: 1 };
}
TypeScript complains on the super.data part:
Class field 'data' defined by the ...
1
vote
1
answer
63
views
Subclass that throws custom error if modified
What's the best way in python to create a subclass of an existing class, in such a way that a custom error is raised whenever you attempt to modify the object?
The code below shows what I want.
class ...
1
vote
0
answers
72
views
How to tell Python which constructor to use to instantiate subclasses? [duplicate]
I am in the following situation: I have a type A, two subtypes B and C of A, a function f of type A -> A that is such that
for any B object x, f(x) is of type B;
for any C object x, f(x) is of ...
0
votes
1
answer
28
views
what is subclass tf.keras.layers.Layer instead of using a Lambda in this code?
An example of replacing a lambda with a subclass in code is as follows:
scale = tf.Variable(1.)
scale_layer = tf.keras.layers.Lambda(lambda x: x * scale)
Because scale_layer does not directly track ...
1
vote
1
answer
107
views
Error with custom exception in C++ module (conflicting definition of __cxa_throw)
I get the following error when creating a custom exception in a C++ module:
C:/Sync/Engine/Chorus/Base/win_9x.cpp:600:59: error: conflicting declaration of C function 'void __cxa_throw(void*, void*, ...
0
votes
1
answer
130
views
How to randomize ship positions in a Battleships game?
I'm writing a battleships game for my college project and I want to randomize the positions of the ships in the beginning of the game. My program has an abstract superclass of Ship which branches out ...
1
vote
2
answers
74
views
Is there an elegant way to subclass a Python list which keeps its subclass when adding and slicing, etc
I want to extend the functionality of a standard list, subclassing seems like the obvious way. Actually, I'm using this to store a mathematical expression in Reverse Polish Notation (RPN) for a ...
2
votes
2
answers
243
views
In Java, avoiding null dereferences when calling super(...) in constructor
I am using Java 21.
I have two classes:
abstract class MySuperClass {
private final Object mySuperField;
MySuperClass(Object myField) {
this.mySuperField = myField;
}
public ...
-1
votes
2
answers
55
views
Error while building a program which should output assigned domino tiles for each player in Java
So, I am a total beginner in Java. I need a program that takes a number of players as an input. Then assuming there are 91 domino tiles each with two squares with dots ranging from 0 to 12 in each ...
1
vote
0
answers
114
views
sparql query vs snap sparql query in protege
I am trying to understand the difference between SPARQL Query and Snap SPARQL Query tabs on Protege Desktop version.
I want retrieve all the direct subclasses of a certain class, say class A, and not ...
0
votes
0
answers
35
views
dart instantiate subclass from super
I want a group of classes that are enum-like; we can't subclass enum, so a small class with one final int will do (const even better). I want the superclass to define some default behaviors, and let ...
1
vote
0
answers
137
views
Python pydantic validation of subclasses
I'm trying to use pydantic together with ABC. I created a base class Datasource and four subclasses:
from pydantic import BaseModel
from typing import Literal, Union, TypeVar
from devtools import ...
0
votes
4
answers
209
views
In Ruby, how to count the number of instances created (including subclasses)?
The following class Point contains a class instance variable @count that counts the number of instances created. It seems to work fine:
class Point
@count = 0
class << self
...
1
vote
1
answer
39
views
Is there a way to ensure a function is called for every instance of a subclass inheriting from a base class?
OptionsAndAnswer is the base class.
abstract class OptionsAndAnswer(
...,
open val options: List<Option>
){
fun validate() {
require(options.map { it.id }.distinct().size == ...
0
votes
0
answers
413
views
Cannot call build() method on subclassed Tensorflow model
I'm using the subclassing API in Tensorflow to build a simple MLP model.
from tensorflow.keras import Model
from tensorflow.keras.optimizers import AdamW
from tensorflow.keras.layers import Dense
...
2
votes
2
answers
101
views
Preserving DataFrame subclass type during pandas groupby().aggregate()
I'm subclassing pandas DataFrame in a project of mine. Most pandas operations preserve the subclass type, but df.groupby().agg() does not. Is this a bug? Is there a known workaround?
import pandas as ...
1
vote
1
answer
41
views
Why is it whenever I create a sub class I am unable to access particular functions in Pycharm?
Here I have created a Car Class. I have also created another class called Battery and then a sub class ElectricCar that takes from the Car class. I then begin to create an Instance Tesla then I ...
0
votes
1
answer
88
views
Can you call the method of a parent class in a way that it references a subclass with the same name as a class it depends on?
I have a Game that references classes HouseRules, Card, and Player. I'd like to add functionality to each class.
I've made a subclass called Game and subclasses called HouseRules, Card, and Player ...
1
vote
2
answers
80
views
How to collect all leaf subclasses of a parent class in Python?
Given a base class named ParentClass, how can I find all leaf subclasses of it? For example, if I have:
class ParentClass:
pass
class SubClass1(ParentClass):
pass
class SubClass2(ParentClass)...
0
votes
1
answer
72
views
Do PyPy int optimizations apply to classes subclassed from int?
From what I recall, PyPy has special optimizations on built-in types like ints and lists, which is great because they are very common, and it would be wasteful to treat an int like any other object.
...
1
vote
1
answer
41
views
Pickling subclass does not save new instance attribute
Pickling a subclass that adds a new attribute doesn't seem to include the new attribute. For example:
import requests
import pickle
class MySession(requests.Session):
def __init__(self, *args, **...
1
vote
1
answer
440
views
Can mapstruct @SubclassMapping annotation be combined with an @Mapping for the subclass?
Using mapstruct v. 1.5.5, java v. 22.
Let's say I have some entities that I am wanting to map to DTOs. The DTOs are java records.
My base entity looks like this (simplified):
public abstract class ...
0
votes
1
answer
100
views
Strange inheritance behavior with base class
I hope someone can help me since can't quite understand how it is possible that the following code can work.
I have a base class with some classes that derive from it.
Each derived class has its own ...
0
votes
1
answer
144
views
Subclass super().__init__(*args, **kwargs) not working. Says object.__init__() takes exactly one argument (the instance to initialize) when it doesn't
I am working on making a subclass of the rustworkx.PyGraph class, and I am getting some strange behavior. Namely, it does not allow me to pass keyword arguments to my class.
Here is a minimal example:
...
-1
votes
1
answer
77
views
Subclasses init method
I have a question regarding the init method for subclasses in Python.
In the image above, a child class Developer is being defined from a parent class Employee. In Developer, the __init__ method is ...
1
vote
3
answers
100
views
Simulate an LFU cache
I am trying to simulate a LFU cache. The task is simple; with any new request I want the cache to print out the key of the request and whether it was a HIT or a MISS. E.g.10 MISS 10 HIT 20 MISS 10 HIT ...
-1
votes
1
answer
90
views
How to call a child method after casting an instance from parent to child class?
If I have a class B that inherits from class A and overrides one of its methods, and I have, in TypeScript:
const a: A = new A();
(a as B).toto();
Why is the toto() method that will be called the one ...
0
votes
1
answer
50
views
Is there a way to access a function of subclass by any other class?
I have the superclass A, and its subclass B and C.
I have the class D, and its member:
A* aArray[20]
but in aArray, there will be just B and C objects.
I want to implement a function that will count ...
-3
votes
3
answers
109
views
Why can’t a superclass call a subclass’s methods?
I’m a computer science teacher at a high school, and I’m teaching polymorphism in Java. My students asked me this question, and I didn’t have a good answer.
Let’s say I have an Employee class. Also, I ...
0
votes
1
answer
35
views
print organization name with at least three subset (have at least three organization in subset)
I write a query to solve this problem,
Print Organization name that have at least three organization subset.
SELECT DISTINCT ?Name
WHERE {
?organization a foaf:Organization .
?organization ...
0
votes
2
answers
214
views
Strange behaviour using __init_subclass__ with multiple modules
Inspired by an answer about a plugin architecture, I was playing around with PEP-487's subclass registration and found that it led to surprising results when changing the code slightly.
The first step ...
1
vote
1
answer
27
views
Multiple threading.Threads created using loop inside class can´t store right parameters
I wrote a Threader class using the threading module that creates multiple threads to perform a task (function) on a list of elements. I´m passing slices of said list into a threading.Thread object ...
0
votes
2
answers
44
views
Python sub-class that is inherited by multiple classes
Currently I have a class-definition that looks something like this:
class cls_A:
def __init__(self, a1, a2, a3):
x = a1 + a2
y = a2 + a3
self.__plot__(self)
# some ...
1
vote
1
answer
106
views
If a subclass has no constructor, and neither does the superclass, then why can I construct an instance of the subclass?
From https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html:
"You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler ...
0
votes
1
answer
686
views
How to use Pydantic Sub-Class Field Typing?
I want to validate a field in a pydantic model that is a subclass of another model. The class assigned to this field can change, but will always be a subclass of this specific model.
I am getting an ...
0
votes
1
answer
120
views
How to make method of subclass inherited from pathlib.Path return Path instead of the subclass
Let's say I have defined a subclass inherited from Path, and I want to use methods from Path. For example, Path.glob() here:
from pathlib import Path
class PhotoDir(Path):
pass
# some custom ...
0
votes
0
answers
54
views
How do I add value to my Method call from my Sub class to Main?
The string value seems to appear yet I can not add a value to my calculations in the fulltime and part time.
This is the Main class for my inheritance
import java.util.Scanner;
public class ...
1
vote
1
answer
486
views
Override Property Typing of Lit-Element Subclasses in TypeScript
When subclassing a lit-element Class to add further typing, should the @property decorator be overridden or just the type and initializer? In otherwords, suppose I have this code:
interface AB {
a:...
2
votes
3
answers
510
views
How to make an array of a parent class, made out of different subclasses
I have a class Item, and there are two classes derived from it Sword and Shield.
I want my player to have an array of items:
class Item
{
int x;
};
class Sword : public Item
{
int sharpness =...
1
vote
1
answer
54
views
Subclassing numpy array for symmetric matrices
I want to create a numpy subclass specialized for symmetric matrices.
I want it to be just like any numpy array with only 2 differences:
It will have 2 fields for its eigendecomposition: U, D.
On ...
-2
votes
1
answer
121
views
Getting methods from a class that extends another class [closed]
I have a class that extends another class. Depending on a user's input, if the input "A" then the class needs to be a subclassA, but if they input "B" it will be a subclassB. I ...
0
votes
1
answer
78
views
How to get the lowest subclass of an instances in an rdf graph?
Imagine a ontology with several classes and subclasses. Now i use another namespace to instantiate for example a MathTeacher. MathTheacher is a subclass of Teacher and Teacher a subclass of Job.
Now i ...
0
votes
0
answers
34
views
Java subclass vs superclass inherited variables clarification [duplicate]
ABOUT ME:
I am learning Java because I want to become a software developer someday and I came across the topic of subclasses and their ability to inherit from superclasses on tutorialspoint.com.
where ...
0
votes
1
answer
255
views
How to extend/wrap/intercept/decorate a class (with custom functionality like tracing)
Question
What is the current (~2024) solution to extend/wrap/intercept/decorate a typescript class with additional behaviour to separate concerns?
There are various questions regarding wrapper classes,...
1
vote
1
answer
700
views
Jasmine access protected property
I'm now to angular and have just completed my first implementation of angular inheritance.
I need to write a test case for the subclass where I cam assign value to protected variable (coming from base ...