does python support overloaded methods?(py newbie)

Hung Jung Lu hungjunglu at yahoo.com
Tue Jan 1 02:58:45 EST 2002


Hi,

There are many ways of achieving the things you want to do. But method
overloading itself is a non-Pythonic concept, since Python is a
dynamicly-typed language: you don't know, nor do you care about, the
type of an object until the moment you use it.

One possibility is to use the 'exec' command.

#----------------------------------------------------
class Currency:
    pass

class Dollar(Currency):
    pass

class Euro(Currency):
    pass

class CalculateRupeeEquivalent:

    def calculate(self, currency):
        exec 'result = self.calculate_%s(currency)' %
currency.__class__.__name__
        return result
    
    def calculate_Dollar(self, currency):
        return 48.2400 * currency.amount

    def calculate_Euro(self, currency):
        return 42.9110 * currency.amount

if __name__ == '__main__':
    dollarPortfolio = Dollar()
    euroPortfolio = Euro()
    calculator = CalculateRupeeEquivalent()
    dollarPortfolio.amount = 100.00
    euroPortfolio.amount = 10.00
    print 'Dollar portfolio in Rupees =',
calculator.calculate(dollarPortfolio)
    print 'Euro portfolio in Rupees =',
calculator.calculate(euroPortfolio)
#----------------------------------------------------
output:
Dollar portfolio in Rupees = 4824.0
Euro portfolio in Rupees = 429.11
#----------------------------------------------------

Of course, your example is really just to show a point... for real
currency calculations no one would use subclasses for currency
instances... currency instances are, naturally, instances of the
currency class! :) No subclasses needed.

Depending on how automated and how optimized you want the code to be,
there are many other possible solutions in Python. Python just has too
many dynamic features, which are far more powerful than method
overloading of staticly-typed languages. Sorry for not showing more
implementations, the possibilities are nearly endless in Python.
Truly, if you are trying to emulate method overloading, you are not
thinking in the right Pythonic way. :)

A more Pythonic implementation would be:

#----------------------------------------------------
class Currency:
    fx_rate = None
    pass

class Dollar(Currency):
    fx_rate = 1.00000

class Euro(Currency):
    fx_rate = 1.12423 
    def warning(self):
        print '(Euro should only be used after January 1, 2002!)'

class CalculateRupeeEquivalent:

    fx_rate = 48.2399 

    def calculate(self, currency):
        result = currency.amount / currency.fx_rate * self.fx_rate
        if hasattr(currency, 'warning'):
            currency.warning()    
        return result
    
if __name__ == '__main__':
    dollarPortfolio = Dollar()
    euroPortfolio = Euro()
    calculator = CalculateRupeeEquivalent()
    dollarPortfolio.amount = 100.00
    euroPortfolio.amount = 10.00
    rupee_amount = calculator.calculate(dollarPortfolio)
    print 'Dollar portfolio in Rupees =', rupee_amount
    rupee_amount = calculator.calculate(euroPortfolio)
    print 'Euro portfolio in Rupees =', rupee_amount
#----------------------------------------------------
output:
Dollar portfolio in Rupees = 4823.99
(Euro should only be used after January 1, 2002!)
Euro portfolio in Rupees = 429.092801295
#----------------------------------------------------

Anyway, you get the idea. You are able to check the class features in
runtime, which allows you: (1) a larger body of shared code, and yet
(2) still be able to perform class-specific actions. After you learn
all the dynamic features of Python, you will see that many of the
claimed C++ or Java features are, actually, limitations of those
languages. :)

regards,

Hung Jung



More information about the Python-list mailing list