What is the maximum number of methods in a Python class?

I automatically generate unit tests for some thousands of Python code. The unittest module uses classes to contain tests, but I assume that there is an upper limit on the number of methods a class can contain - is that so?

+4
source share
2 answers

Methods (and virtually all attributes) of the class are stored in a dict . There is no limit to the number of elements that a dict can contain, except that each key must be unique.

+8
source

I strongly doubt that you ever reached the limit, even if it were. As far as I know, the number of methods that an object can have is limited only by memory. I just defined a class with a million functions, no problem. Try it if you do not believe me:

 >>> class C(object): pass >>> for i in xrange(10**6): exec('C.func%d=lambda self: %d'%(i,i)) >>> c = C() >>> c.func1() 1 >>> c.func999999() 999999 

If your class has more than a million functions (hell or more than a dozen or so), you have other problems.

+6
source

Source: https://habr.com/ru/post/1342737/


All Articles