Skip to main content
7516 votes
26 answers
1.2m views

What are metaclasses? What are they used for?
Bite code's user avatar
  • 600k
1534 votes
32 answers
1.4m views

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.
fuentesjr's user avatar
  • 52.5k
4 votes
3 answers
180 views

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):...
LucasL's user avatar
  • 1
2031 votes
12 answers
1.7m views

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.
Dan's user avatar
  • 35.8k
374 votes
21 answers
315k views

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 (&...
Staale's user avatar
  • 28.1k
3 votes
3 answers
390 views

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 ...
steveo225's user avatar
  • 12k
214 votes
14 answers
159k views

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__....
Hannah S.'s user avatar
  • 3,370
117 votes
14 answers
42k views

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 ...
astrofrog's user avatar
  • 34.4k
147 votes
5 answers
117k views

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 ...
nicotine's user avatar
  • 2,679
126 votes
13 answers
99k views

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(...
Jesse Aldridge's user avatar
133 votes
4 answers
64k views

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 ...
Falmarri's user avatar
  • 48.7k
70 votes
11 answers
49k views

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

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 ...
concentriq's user avatar
-1 votes
1 answer
180 views

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: ...
isswf's user avatar
  • 191
0 votes
2 answers
99 views

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 ...
TheOnlyWayUp's user avatar

15 30 50 per page
1
2 3 4 5
7