How to "generalize" a function?

Dan Sommers me at privacy.net
Sun Apr 24 19:16:44 EDT 2005


On Sun, 24 Apr 2005 23:40:22 +0200,
Thomas Köllmann <koellmann at gmx.net> wrote:

> Hi, everybody!
> I'm teaching myself Python, and I have no experience in programming
> apart from some years of shell scripting. So, please bear with me.

> These two funktions are part of an administrative script I've set
> myself as a first lesson, and, as you will see, they're practically
> the same, except for one variable. So I'd like to weld them together
> -- but I can't find out how to.

[ two very similar functions snipped ]


> I feel it should be possible to use something like

> def writeFile(ip,keyword):
>     ...

Absolutely.

> but how would I construct expressions like

>     netmaskLineNum = conf.index(netmaskLine)

> in that case (netmask being the keyword here)?

netmaskLineNum and addressLineNum are just names.  They may mean
something to you and to people who read your program, but they mean
nothing to Python.  So just use generic names:

    for line in conf:
        if regex.search( line )
            theLine = line
    theLineNum = conf.index( theLine )

    etc.

HTH,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>
μ₀ × ε₀ × c² = 1



More information about the Python-list mailing list