Skip to main content
Filter by
Sorted by
Tagged with
7516 votes
26 answers
1.2m views

What are metaclasses? What are they used for?
Bite code's user avatar
  • 600k
100 votes
7 answers
43k 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 ...
catherine's user avatar
  • 22.8k
9 votes
3 answers
32k views

Consider this json file named h.json I want to convert this into a python dataclass. { "acc1":{ "email":"[email protected]", "password":"...
Kanishk's user avatar
  • 368
7 votes
1 answer
5k views

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 ...
Richard's user avatar
  • 3,509
5 votes
2 answers
1k views

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): ... ...
WW00WW's user avatar
  • 447
4 votes
3 answers
238 views

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(...
SultanOrazbayev's user avatar
4 votes
1 answer
2k views

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 ...
FR_MPI's user avatar
  • 141
4 votes
1 answer
5k views

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. ...
jack.py's user avatar
  • 442
3 votes
1 answer
3k views

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 = ...
brightstar2100's user avatar
3 votes
3 answers
2k views

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 ...
Mathew Carroll's user avatar
3 votes
1 answer
6k views

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:...
Anakhand's user avatar
  • 3,128
3 votes
2 answers
2k views

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. ...
xrisk's user avatar
  • 3,918
3 votes
1 answer
198 views

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): ...
mon's user avatar
  • 23k
2 votes
3 answers
692 views

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)...
codeananda's user avatar
  • 1,448
2 votes
2 answers
755 views

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,...
Jacob Belanger's user avatar
2 votes
1 answer
956 views

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 = ...
PokerFaCe's user avatar
  • 428
2 votes
1 answer
116 views

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 ...
Akira's user avatar
  • 2,808
2 votes
1 answer
982 views

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 ...
Milano's user avatar
  • 18.9k
2 votes
1 answer
7k views

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 ...
LZR_13's user avatar
  • 45
2 votes
1 answer
1k views

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 ...
LightCC's user avatar
  • 12k
2 votes
1 answer
540 views

class _Edges(defaultdict): def __missing__(self, vertex): self.setdefault(vertex, True) def __delitem__(self, dst): self[dst] = False def del_vertex(self, dst): ...
DivinusMessor's user avatar
2 votes
0 answers
66 views

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 ...
Randumb_ASH's user avatar
2 votes
1 answer
44 views

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['...
Victor's user avatar
  • 23
2 votes
0 answers
450 views

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 ...
Jamie.Sgro's user avatar
2 votes
0 answers
39 views

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 ...
mavaras's user avatar
  • 63

1
2 3 4 5
11