Payal wrote: > Can someone please give a very simple example of using __new__ wherein > __init__ cannot be used?
Subclasses of immutable types, e. g. tuple: >>> class A(tuple): ... def __init__(self, a, b): ... pass ... >>> a = A(1,2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: tuple() takes at most 1 argument (2 given) >>> class A(tuple): ... def __new__(cls, a, b): ... return tuple.__new__(cls, (a, b)) ... >>> A(1, 2) (1, 2) >>> type(_) <class '__main__.A'> Peter _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
