923 questions
2 votes
3 answers
180 views
How to correctly type-annotate a classmethod-like descriptor in python?
classonlymethod decorator/descriptor defined below is like the built-in classmethod, but you're not allowed to call the method on instances, only on the class itself. from typing import Concatenate, ...
2 votes
1 answer
69 views
How to pass any object as a classmethod's first argument - circumvent injection of class argument
Passing a class, or a totally different object, as the first argument to method is easy: class Foo: def method(self): ... Foo.method(object()) # pass anything to self I wonder, is this possible ...
1 vote
1 answer
115 views
Is it possible to mix ctypes.Structure with regular python fields?
Just going the straight way like this, doesn't seem to work in python 3. import ctypes class cA(ctypes.Structure): _fields_ = [("a", ctypes.c_ubyte)] def __init__(self): ...
1 vote
1 answer
85 views
Access `self` in classmethod when instance (self) calls classmethod
Is it possible to access the object instance in a Python @classmethod annotated function when the call occurs via the instance itself rather than the class? class Foo(object): def __init__(self, ...
2 votes
1 answer
810 views
Purpose of @classmethod decorator in Pydantic validators
Of the many examples of using @field_validators and @model_validators in Pydantic's documentation, the @classmethod decorator is frequently applied to the method first. Here is one of the examples ...
2 votes
1 answer
80 views
Conceptual issues about responsibility with classmethod factories for inheritance in Python
As far as I understand, the difference between @classmethod and @staticmethod is the cls parameter which is used so a class can construct instances of subclasses. I have encountered some issues with ...
0 votes
0 answers
32 views
Django Admin Custom Add
Say I have a model called Book: class Book(EOModel): title = models.CharField(max_length=100, null=False, blank=False) subtitle = models.CharField(max_length=100, null=True, blank=True) ...
0 votes
1 answer
30 views
ValidateSet on class instance method param; Powershell 5.1
ValidateSet is a great feature in Powershell 5.1. Unfortunately, you cannot use it on parameters in class instance (object) functions. Is there a way to force the use of ValidateSet anyways? Note: ...
0 votes
1 answer
49 views
Why would the run method of a Thread subclass be a class method?
I'm reading through the documentation for the schedule API and in the "Run in the Background" example they define the run method as a class method in their ScheduleThread class. The code is ...
0 votes
2 answers
47 views
python recursion error, calling classmethod of derived class in __new__ method
I have the following piece of code - simplified version of what I want to do. But it gives me a recursion error. class File: def __new__(cls, filename): extension = filename.split(".&...
5 votes
3 answers
135 views
invoke a `@classmethod`-annotated method
Dummy scenario: # where I want to collect my decorated callbacks cbs = [] # decorator to mark a callback to be collected def my_deco(cb): cbs.append(cb) return cb class C: @my_deco @...
0 votes
0 answers
82 views
Python unittest.mock, wrap instance method, turn mock on/off
I would like to write an unit test for an instance method of a class under test containing some simple logic but calling another more complicated instance method. I would like to wrap the complicated ...
0 votes
0 answers
26 views
Classmethod to check Initialization of Instance variables in python
Is it bad practice to performm a try/except check within a classmethod and return an instance only if everything went well, or should I check everything inside __init__()? Alternatively, is there ...
0 votes
2 answers
55 views
How to compare two integer fields in a model method in django?
I want to compare two integer fields inside a method in a django model. The two fields are not correctly getting compared and it is throwing an error for example: total_slots = models.IntegerField(...
0 votes
3 answers
86 views
Putting if statement in Ruby create
Example.create( attribute1 = "asdf", attribute2 = "asdf2", attribute3 = "and 20 more attributes" ) But how do I conveniently make variable2 = "qwer" if ...