How to overload __init__ method based on argument type?

A much neater way to get ‘alternate constructors’ is to use classmethods. For instance: >>> class MyData: … def __init__(self, data): … “Initialize MyData from a sequence” … self.data = data … … @classmethod … def fromfilename(cls, filename): … “Initialize MyData from a file” … data = open(filename).readlines() … return cls(data) … … @classmethod … … Read more

Why doesn’t Java offer operator overloading?

There are a lot of posts complaining about operator overloading. I felt I had to clarify the “operator overloading” concepts, offering an alternative viewpoint on this concept. Code obfuscating? This argument is a fallacy. Obfuscating is possible in all languages… It is as easy to obfuscate code in C or Java through functions/methods as it … Read more

What are the basic rules and idioms for operator overloading?

Common operators to overload Most of the work in overloading operators is boiler-plate code. That is little wonder, since operators are merely syntactic sugar, their actual work could be done by (and often is forwarded to) plain functions. But it is important that you get this boiler-plate code right. If you fail, either your operator’s … Read more