Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28967

Re: avoid the redefinition of a function

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <torriem@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'function,': 0.07; 'python': 0.09; 'called.': 0.09; 'globals': 0.09; 'objects.': 0.09; 'looked': 0.10; 'def': 0.10; '(like': 0.15; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'redefinition': 0.16; 'wrote:': 0.17; 'define': 0.20; 'script': 0.24; 'allows': 0.25; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:[ 10': 0.26; 'am,': 0.27; 'skip:# 10': 0.27; 'functions.': 0.27; 'steps:': 0.29; 'function': 0.30; 'stuff': 0.30; 'problem.': 0.32; 'suggestion': 0.32; 'print': 0.32; 'function.': 0.33; 'to:addr:python-list': 0.33; 'list': 0.35; 'there': 0.35; 'list.': 0.35; 'add': 0.36; 'received:org': 0.36; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'should': 0.36; 'why': 0.37; '(for': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'step': 0.39; 'received:192': 0.39; 'called': 0.39; 'little': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'descriptive': 0.84; 'grew': 0.91
X-Virus-Scanned amavisd-new at torriefamily.org
Date Wed, 12 Sep 2012 07:52:15 -0600
From Michael Torrie <torriem@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:10.0.7) Gecko/20120824 Thunderbird/10.0.7
MIME-Version 1.0
To python-list@python.org
Subject Re: avoid the redefinition of a function
References <CAOuJsMnLf4P9p_rZ4p1W-SndkSOV2WK_v5j1AET085LZ6bdavQ@mail.gmail.com>
In-Reply-To <CAOuJsMnLf4P9p_rZ4p1W-SndkSOV2WK_v5j1AET085LZ6bdavQ@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.563.1347459014.27098.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1347459014 news.xs4all.nl 6842 [2001:888:2000:d::a6]:49163
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28967

Show key headers only | View raw


On 09/12/2012 06:56 AM, Jabba Laci wrote:
> I have an installer script that contains lots of little functions. It
> has an interactive menu and the corresponding function is called. Over
> time it grew long and when I want to add a new function, I should give
> a unique name to that function. However, Python allows the
> redefinition of functions:
> 
> #!/usr/bin/env python
> 
> def step_1():
>     print 1
> 
> def step_1():
>     print 2
> 
> step_1()
> 
> This will call the 2nd function. Now my functions are called step_ID
> (like step_27(), step_28(), etc.). How to avoid the danger of
> redefinition? Now, when I write a new function, I search for its name
> to see if it's unique but there must be a better way.

I don't understand the other poster's suggestion to your problem.  I
have looked at your script and think I understand how you are using
them.  Keep in mind that functions in python are just objects.  So
rather than define them as step_##() and then search the globals list
for them, why not just define them as descriptive functions (for
example, "install_java") and then put them into a list.

For example:

def install_java():
   pass

def install_tomcat():
   pass


steps = [install_java, install_tomcat, etc]
# or steps.append(install_java) if steps already has stuff in it, etc

# then:
for step in steps:
   step()

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: avoid the redefinition of a function Michael Torrie <torriem@gmail.com> - 2012-09-12 07:52 -0600

csiph-web