Skip to main content

All Questions

Tagged with
0 votes
1 answer
54 views

What are the differences between using mutiple or'ed typehints vs abc and an inheritance hierachy in Python?

Python is a dynamic language. This means that types are dynamic at runtime and Python makes use of the concept of Ducktyping. What this means is that for any object x was can do x.some_function() x....
FreelanceConsultant's user avatar
0 votes
0 answers
27 views

Assigning my custom class to an object/widget from a .ui file [duplicate]

Writing my 1st code in python, how can I create an instance of a class of my own and assign it to an object/widget from a .ui file? Something like this: import sys from PyQt6.uic import loadUi from ...
eli's user avatar
  • 29
0 votes
2 answers
62 views

multiple inheritance without defining a class [duplicate]

I'm not sure that's possible, and I'm wondering if that's the way to go, or if there exists a better way of doing that. I'll create a toy example to expose what I'm looking for. Suppose I have people ...
Elerium115's user avatar
2 votes
1 answer
48 views

How does __slots__ inheritance work? (My code works and I don't know why)

Here is the example of my implementation: class FileHandler: __slots__ = ("_target_path",) def __init__(self, target_path: Path) -> None: self._target_path = target_path ...
Todor Naydenov's user avatar
1 vote
3 answers
75 views

If I already have an instance of a super class, how can I create an instance of a child class from it?

How do I properly inherit data from an instantiation of a super class? For example, consider something I can get to work, but is confusing in its behaviour: from dataclasses import dataclass from ...
bzm3r's user avatar
  • 3,770
0 votes
2 answers
41 views

Pydantic Inheritance Defaults

How can I typehint a parent BaseModel such that a child subclass can provide defaults for some of the fields: from pydantic import BaseModel class Parent(BaseModel): first: str second: str ...
Oliver's user avatar
  • 335
2 votes
1 answer
34 views

Python Django access fields from inherited model

Hi I have a question related to model inheritance and accessing the fields in a Django template. My Model: class Security(models.Model): name = models.CharField(max_length=100, blank=False) class ...
Dennis's user avatar
  • 99
0 votes
1 answer
42 views

sql Alchemy models inheritance

I use Python and SQLAlchemy 2.0.23. My aim is to dynamically create SQLAlchemy models and tables in postgre database based on these models. I have several countries, and these are similar tables for ...
Normunds's user avatar
2 votes
2 answers
132 views

MyPy displays an error when inheriting from str and adding metaclass

Here's simple example of code: class meta(type): pass class Test(str, metaclass = meta): pass When I run mypy on it (just from cli, without any flags or other additional setup) I'm seeing next output:...
Ikor Jefocur's user avatar
0 votes
0 answers
20 views

Bug with diamon-shaped inheritance in Python [duplicate]

I am trying to use inheritance in which I have a base class, say X, and 2 classes which both independently inherit from X, say A(X) and B(x) (these represent the common attributes of a whole bunch of ...
johnstone.matt's user avatar
1 vote
2 answers
34 views

How do I force instantiation of the child class and not the parent class?

I have two modules, one.py: class A: def __init__(self): pass class B: def __init__(self): pass def assign_A(self): self.A = A() and two.py: import one class A(...
freitreppe's user avatar
0 votes
2 answers
69 views

Can a nested python class inherit from its enclosing class

I have checked several questions that seem similar, but none fits my bill: is it legal python to define the enclosing class as the super class of an inner class like this class outer: def say_hi():...
user2961818's user avatar
1 vote
1 answer
40 views

If Python builtins derive from ABCs, then why is their metaclass type instead of ABCMeta?

I was reading PEP-3119, and I discovered that builtins derive from ABCs. From PEP-3119: The built-in type set derives from MutableSet. The built-in type frozenset derives from Set and Hashable. In ...
Jordan's user avatar
  • 306
0 votes
1 answer
33 views

Functions inside inherited Cheetah template can't see global variables

I have some Cheetah templates that are structured using inheritance. basepage.tmpl: from quixote.publish import get_session() #set global $session = get_session() #block content <!DOCTYPE html>...
Colin 't Hart's user avatar
1 vote
0 answers
57 views

Using the @override method in my package? Issues and workarounds?

In python, I am developing a package which has the following two classes: I have a parent class: class Parent: def my_method(self): return 1 and I have a child class that overrides this ...
Atharva's user avatar
  • 181

15 30 50 per page
1
2 3 4 5
309