510 questions
7516
votes
26
answers
1.2m
views
What are metaclasses in Python?
What are metaclasses? What are they used for?
100
votes
7
answers
43k
views
What are the advantages of class-based views compared to function-based views?
I always use FBVs (Function Based Views) when creating a django app because it's very easy to handle. But most developers said that it's better to use CBVs (Class Based Views) and use only FBVs if it ...
9
votes
3
answers
32k
views
How do I convert a json file to a python class?
Consider this json file named h.json I want to convert this into a python dataclass.
{
"acc1":{
"email":"[email protected]",
"password":"...
7
votes
1
answer
5k
views
Python nested dataclasses ...is this valid?
Background
I'm using dataclasses to create a nested data structure, that I use to represent a complex test output.
Previously I'd been creating a hierarchy by creating multiple top-level dataclasses ...
5
votes
2
answers
1k
views
Keyword arguments in python class inheritance
Hello I am recently looking about the metaclass in Python.
I learned about we can attach a metaclass to a class in this way:
class Metaclass(type):
...
class MyClass(metaclass=Metaclass):
...
...
4
votes
3
answers
238
views
How to correctly define a classmethod that accesses a value of a mangled child attribute?
In Python, how do I correctly define a classmethod of a parent class that references an attribute of a child class?
from enum import Enum
class LabelledEnum(Enum):
@classmethod
def list_labels(...
4
votes
1
answer
2k
views
Create multiple instances of pybullet client within a python class
I am using pybullet in a python class. I import it as import pybullet as p.
When I have several instances of the class using pybullet, is the class p the same for each instance or is the "variable" p ...
4
votes
1
answer
5k
views
How to use self in an abstract class implementation in Python?
I'm working on a project using abstract classes in Python (specifically, the abc module).
I have a few implementations of this abstract class, which have their own constructors and need to use self.
...
3
votes
1
answer
3k
views
How can I make __str__ or __repr__ return the current class's name? [duplicate]
I have this code:
class Employee:
def __init__(self, name, pay, gender):
self.name = name
self.pay = pay
self.gender = gender
def add_raise(self):
self.pay = ...
3
votes
3
answers
2k
views
Python (pandas): Using decorators using pandas API
I'm quite new to decorators and classes in general on Python, but have a question if there is a better way to decorate pandas objects. An an example, I have written the following to create two methods ...
3
votes
1
answer
6k
views
Enum class with type `type` as mixin
The docs state that it is possible to make an enumeration class whose members are exactly of a certain type by adding that type as a mixin to the enum class:
While :class:IntEnum is part of the :mod:...
3
votes
2
answers
2k
views
Wrapping derived class method from base class
Imagine I have a base and a derived class like so:
class A:
def foo(self):
pass
class B(A):
def foo(self):
pass
I wish to wrap calls foo calls made by instances of B. ...
3
votes
1
answer
198
views
Why does this parent class setter call use type(self) rather than self?
Python @property inheritance the right way explains how to call the parent setter.
class Number:
def __init__(self):
self._value = None
@property
def value(self):
...
2
votes
3
answers
692
views
Can you modify an object's field every time another field is modified?
I have a dataclass that looks like this
from dataclasses import dataclass, field
@dataclass
class Data:
name: str | None = None
file_friendly_name: str | None = field(default=None, init=False)...
2
votes
2
answers
755
views
Function Chaining in Python, ignore rest of chain if a segment returns False
The title pretty much explains the problem. I don't know if there's a practical solution to this or if I'm being too picky over the behavior of my code.
This article was hinting in the right direction,...
2
votes
1
answer
956
views
calling a class below of another class
I'm working on a django project with rest_framework and I have a problem with serializers. Here is my code:
class CategorySerializer(serializers.ModelSerializer):
featured_product = ...
2
votes
1
answer
116
views
How to access data in this model class?
I'm using package libmf to do parallel non-negative matrix factorization, i.e., X = WH. I use the method fit from the class MF. As mentioned in below description, the resulting matrices are stored in ...
2
votes
1
answer
982
views
decorator that decorates @property and take arguments
My class (dataclass) has many properties that are calculations based on other properties or dataclass fields.
I'm trying to create a decorator that takes a list of required fields or properties. That ...
2
votes
1
answer
7k
views
Python's asyncio.Event() across different classes
I'm writing a Python program to interact with a device based on a CAN Bus. I'm using the python-can module successfully for this purpose. I'm also using asyncio to react to asynchronous events. I have ...
2
votes
1
answer
1k
views
Is there any reason to use collections.namedtuple over typing.NamedTuple in Python?
I have used collections.namedtuple in some code, but now I see that in Python 3.6 there is a new typing.NamedTuple base class available in the new class style. It also appears to make type hints ...
2
votes
1
answer
540
views
Graph Class: TypeError: __init__() takes 1 positional argument but 3 were given
class _Edges(defaultdict):
def __missing__(self, vertex):
self.setdefault(vertex, True)
def __delitem__(self, dst):
self[dst] = False
def del_vertex(self, dst):
...
2
votes
0
answers
66
views
New Dialog Class Incorrect Button Style
I was trying to make a subclass that acts like the original but its go function returned the string of the text of the button, instead of the index of the button.
My own defined subclass makes the ...
2
votes
1
answer
44
views
Python convert class instance variable to nested structure
Defining a class of instance variable from commonly used fields in API response by looping through each JSON record
self.id=response['identifier'] -->output: 1234
self.name=response['...
2
votes
0
answers
450
views
How to create a Python dataclass with a prop that depends on the output from a default_factory
I'm trying to use the dataclasses default_factory field to dynamically add a property (id) while also still being able to create a subsequent property (id_with_appended_str) whose default value ...
2
votes
0
answers
39
views
Python's free variables behaviour with inner classes [duplicate]
This is the challenge:
x = 11
def foo1():
x = 2
class foo:
print(x)
foo1() # prints 2
x = 11
def foo2():
x = 2
class foo:
print(x)
x += 1
foo2() # prints 11
What is the ...