Page 242 -
P. 242

modular programming


           Fully Qualified Names (FQNs) prevent

           your programs from getting confused


           Imagine if you lived in a world where people had first names only:






                  Michael
                                                                                    Michael








                             Hi, it‛s Michael.
                             Say, are you free
                             on Friday night?



           Lots of people share the same first name. But people also have
           surnames. If you use a first name with a surname, things are a lot
           less confusing.

          And it’s the same thing with code. If you have two modules
           containing functions with the same name, the computer will get
           confused. But if you fully qualify the function name, by prefixing   Oh, I need to
           it with the module name, the computer will know exactly what   apply the 10% discount
           you mean:                                                      from promotion.py?
                                                                          That‛s not a problem,
                         promotion.discount(1.75)                         since you‛re using a
                                                                            FQN...

           If you are going to use Fully Qualified Names (FQNs) from a
           module, then you will also need to change the way you import the
           code:



                         from promotion import *
                                                This will import the code from promotion.py
                         import promotion        but to use it, you will need to add “promotion."
                                                 to the start of the function name.         coffee_pos.py


           Now you can fix the code to use both discounts.

                                                                                      you are here 4    207
   237   238   239   240   241   242   243   244   245   246   247