## Python Frequently Asked Questions, Release 3.9.5 

However, a far more straightforward way to get the effect of a static method is via a simple module-level function: 

def getcount(): return C.count 

If your code is structured so as to define one class (or tightly related class hierarchy) per module, this supplies the desired encapsulation. 

## 2.6.9 How can I overload constructors (or methods) in Python? 

This answer actually applies to all methods, but the question usually comes up first in the context of constructors. 

In C++ you’d write 

class C { C() { cout<<"No arguments 
 "; } C( int i) { cout <<"Argument is "<<i <<" 
 "; } } 

In Python you have to write a single constructor that catches all cases using default arguments. For example: 

classC : def __init__ (self, i= None ): if i is None : print("No arguments") else : print("Argument is", i) 

This is not entirely equivalent, but close enough in practice. 

You could also try a variable-length argument list, e.g. 

def __init__ (self,*args): ... 

The same approach works for all method definitions. 

## 2.6.10 I try to use __spam and I get an error about _SomeClassName__spam. 

Variable names with double leading underscores are “mangled” to provide a simple but effective way to define class private variables. Any identifier of the form$_{__spam}$ (at least two leading underscores, at most one trailing under- score) is textually replaced with$_{_classname__spam}$ , where$_{classname}$ is the current class name with any leading underscores stripped. 

This doesn’t guarantee privacy: an outside user can still deliberately access the “_classname__spam” attribute, and private values are visible in the object’s$_{__dict__}$ . Many Python programmers never bother to use private variable names at all. 

## 2.6.11 My class defines __del__ but it is not called when I delete the object. 

There are several possible reasons for this. 

The del statement does not necessarily call$_{__del__()}$ – it simply decrements the object’s reference count, and if this reaches zero$_{__del__()}$ is called. 

If your data structures contain circular links (e.g. a tree where each child has a parent reference and each parent has a list of children) the reference counts will never go back to zero. Once in a while Python runs an algorithm to detect such cycles, but the garbage collector might run some time after the last reference to your data structure vanishes, so your$_{__del__()}$ method may be called at an inconvenient and random time. This is inconvenient if you’re trying 

