The two discount functions have the same name

Here is the promotion . py module you just created:

def discount(price):

return 0.9 * price

And here is the starbuzz .py module:

# Official Starbuzz Discount Module

# Copyright(c) Starbuzz Corporation

# All Rights Reserved.

# This function calculates a 5% discount on a price def discount(price):

return 0.95 * price

Both of the modules define a function called discount (). So what happens when you try to use them? If Python sees a line of code like this:

new price - discount(1.75)

which function will it call? The promotion discount? The Starbuzz discount? Both? Neither???

This is one of the problems of using shared code. Sometimes, there's a function in one module that has the same name as a function in another module. When this happens, the last function imported is the one used, which has the effect of overloading any existing function that has the same name. This can result in to hard-to-find bugs.

So what do you do?

You need to somehow qualify your function names.

Was this article helpful?

0 0

Post a comment