If your signatures differ only in the number of arguments using default arguments is the right way to do it. If you want to be able to pass in different kinds of argument I would try to avoid the isinstance
-based approach mentioned above, instead using keyword arguments. If using just keyword arguments becomes unwieldy you can combine it with classmethods (the bzrlib code likes this approach). This is just a silly example, but I hope you get the idea:
class C(object):
def __init__(self, fd):
# Assume fd is a file-like object.
self.fd = fd
@classmethod
def fromfilename(cls, name):
return cls(open(self.fd, 'rb'))
# Now you can do:
c = C(fd)
# or:
c = C.fromfilename('a filename')
Notice all those classmethods still go through the same init, but using classmethods can be more convenient than having to remember what combinations of keyword arguments to init work.
isinstance
is best avoided because python's duck typing makes it hard to figure out what kind of object was actually passed in. For example: if you want to take either a filename or a file-like object you cannot use isinstance(arg, file)
because there are many file-like objects that do not subclass file
(like the ones returned from urllib, or StringIO, or...). It's usually a better idea to just have the caller tell you explicitly what kind of object was meant, by using different keyword arguments.