104 questions
7516 votes
26 answers
1.2m views
What are metaclasses in Python?
What are metaclasses? What are they used for?
1534 votes
32 answers
1.4m views
Is there a built-in function to print all the current properties and values of an object?
So what I'm looking for here is something like PHP's print_r function. This is so I can debug my scripts by seeing what's the state of the object in question.
4 votes
3 answers
180 views
What happens if __del__ is defined as an async def coroutine in Python (CPython 3.12)?
I'm experimenting with asynchronous patterns in Python and was curious what happens if I define a __del__ method using async def in Python 3.12. For example: class MyClass: async def __del__(self):...
2031 votes
12 answers
1.7m views
Getting the class name of an instance
How do I find out the name of the class used to create an instance of an object in Python? I'm not sure if I should use the inspect module or parse the __class__ attribute.
374 votes
21 answers
315k views
How to get method parameter names?
Given that a function a_method has been defined like def a_method(arg1, arg2): pass Starting from a_method itself, how can I get the argument names - for example, as a tuple of strings, like (&...
3 votes
3 answers
390 views
Using lru_cache on a class that has a classmethod
I want to cache instances of a class because the constructor is complicated and repeated uses won't alter the contents. This works great. However, I want to create a classmethod to clear the cache. In ...
214 votes
14 answers
159k views
Get fully qualified class name of an object in Python
For logging purposes I want to retrieve the fully qualified class name of a Python object. (With fully qualified I mean the class name including the package and module name.) I know about x.__class__....
117 votes
14 answers
42k views
Prevent creating new attributes outside __init__
I want to be able to create a class (in Python) that once initialized with __init__, does not accept new attributes, but accepts modifications of existing attributes. There's several hack-ish ways I ...
147 votes
5 answers
117k views
Implementing slicing in __getitem__
I am trying to implement slice functionality for a class I am making that creates a vector representation. I have this code so far, which I believe will properly implement the slice but whenever I do ...
126 votes
13 answers
99k views
Get class that defined method
How can I get the class that defined a method in Python? I'd want the following example to print "__main__.FooClass": class FooClass: def foo_method(self): print "foo" class BarClass(...
133 votes
4 answers
64k views
Should __ne__ be implemented as the negation of __eq__?
I have a class where I want to override the __eq__ method. It seems to make sense that I should override the __ne__ method as well. Should I implement __ne__ as the negation of __eq__ as such or is it ...
70 votes
11 answers
49k views
How is the 'is' keyword implemented in Python?
... the is keyword that can be used for equality in strings. >>> s = 'str' >>> s is 'str' True >>> s is 'st' False I tried both __is__() and __eq__() but they didn't work. ...
2 votes
1 answer
549 views
best way to create serializable data model
somewhat inexperienced in python. I am coming from C# world, and I am trying to figure out what is the best practice to create a data structure in python that: can have empty fields (None) can have ...
-1 votes
1 answer
180 views
python: create empty dataclass and populate it on demand
python newbie: I need to create an empty data model class, populate it with values and then convert it to JSON string, however i am missing something important here. see error below my data class: ...
0 votes
2 answers
99 views
Python: Magic method for when the object's reference count changes?
I want to a method that runs when object reference count is changed. is there a method like the following code ? class Foo(): def __method__(self): print("ojbect reference count is ...