Python: Why to assign functions’ return values to dummy variables

In response to Kototama’s comment in the previous post:

There is a need, when running directly under the interpreter (code placed in the global namespace, not inside a function or method), to assign the return value of any function to a dummy variable.

This is because when you enter an expression that has a value (other than None) in the interpreter, the value’s representation (“repr”) is printed to standard output.
Example (standard output given in blue):

>>> def add_num(mylist):
    num = max(mylist) + 1
    mylist.append(num)
    return num
 
>>> mylist = [0]
>>> mylist
[0]
>>> add_num(mylist)
1
>>> mylist
[0, 1]

That’s nice, but eventually we’ll do something like this, without giving it much thought:

>>> for i in xrange(10):
    add_num(mylist)
 
2
3
4
5
6
7
8
9
10
11

Eh?! We didn’t want those to be printed at all. Where did they come from?
“for” loop or not, the add_num calls were still directly in the global namespace and not inside a function or method, so the value of the expression was printed.

What I usually do to solve this is to put the code in a main() function and simply calling it in the global namespace.
But in the example I gave in that post, I had to specifically modify code outside main(), hence the need to assign to a dummy variable, to avoid having an expression with a value (unlike C, in Python an assignment is not an expression).

One Response to Python: Why to assign functions’ return values to dummy variables

  1. The moment making a new kitchen or renovating an existing
    kitchen, it is highly recommended and a good idea to keep an open mind and find out everything possible – towards the end of
    the process, it will definitely pay off. Here are some common myths
    about the design process and its particular results:

    “You can certainly add later” -the best planning is to take into account and add all the characteristics of the kitchen at the planning and as well as or renewal stages, and not
    at a later stage. For example, although
    you may are not thinking about setting up a flat screen TV SET in the kitchen space or if you do not currently have a water dispenser, the best thing is to make for future
    installation of these also to plan power, communication and water items and keep the alternatives wide open. Also, since the kitchen is supposed to provide us for quite some time,
    it is a good idea to measure all existing ergonomic solutions such as drawer drawers, cabinet style dishwashers, and so forth Even if
    you are young and fit at the moment, these additions will prove
    themselves Mobility as time goes on. Either way, if you prefer to deal with kitchen planning
    in stages – one aspect or specific area at a time – make certain to plan carefully so that everything will fit
    in stylistic and functional harmony when the kitchen will finally be complete.

    ” I do not have so many things” – whether we like
    it or not, your kitchen has gigantic potential for ruining due to multitude of items kept in it, which is why space is plenty to store, This means that there is a relative absence of
    overhead storage cabinetry, so it is important to balance function and style.

    “Bigger Better” – Your kitchen space is large and you need a sizable kitchen, but
    then you will find yourself going back and out from one
    machine to another, in addition to this situation, preferably
    in the look stages, consider adding a job sink beside the refrigerator
    or Reduce the need to cross these distances, and there
    is without doubt that these enhancements
    mean additional costs, and if your budget is limited, it is obviously
    worthwhile considering a compact kitchen.

    “I do not desire a designer ” -not every kitchen design project requires a designer, but many people need someone to help us see the overall picture and watch over the
    work. Renting consult with an architect or designer does involve quite a
    lot of cost, but in the long run it is around conserving
    money and time. Even though you have a very clear picture of what you want, do not save the expense of
    a designer since it can best fulfill ideal. Proper and efficient planning of your kitchen and coordination between all the different professionals
    included in building your kitchen by a designer can keep your project within the some budget, save you unnecessary money and unneeded
    problems and particularly keep your sanity.

Leave a comment