calling functions
bruno modulix
onurb at xiludom.gro
Tue Aug 2 05:58:53 EDT 2005
anthonyberet wrote:
> This is the first time I have tried out functions (is that the main way
> of making subroutines in Python?)
A function is allowed to change it's arguments and to return None, so
yes, you can consider it as a 'subroutine'.
>
> Anyway, my function, mutate, below
>
> #make a child string by randomly changing one character of the parent
>
> Def mutate():
s/Def/def/
<meta>
please copy-paste code - retyping it increases the risk of typos.
</meta>
> newnum=random.randrange(27)
> if newnum==0:
> gene=' '
> else:
> gene=chr(newnum+96)
> position=random.randrange(len(target))
> child=parent[:position-1]+gene+parent[position+1:]
Where does this 'gene' come from ?-)
> mutate()
>
>
> The trouble is when I later (as in further down the code) attempt to
> retrieve the value of gene I get an error saying that gene is undefined.
Of course it is.
> It works fine when I don't have the routine defined as a function. - the
> IF- Else structure means gene must have a value of ' ' or 'a' to 'z'.
This 'gene' only lives in the function body - as with almost any other
programming language.
> It seems that the line:
>
> mutate()
>
> is not invoking the function,
It is. But this function does not return anything (well, it returns
None, which is the Python representation of exactly nothing) - and you'd
loose it if it did anyway.
<non-pythonic-explanation>
A variable created in a function is local to the function. It disappears
as soon as the function returns - unless you keep a reference to it one
way or another. The usual way to do so is to return the variable to the
caller :
</non-pythonic-explanation>
def mutate():
newnum = random.randrange(27)
if newnum == 0:
gene=' '
else:
gene = chr(newnum + 96)
return gene
gene = mutate()
# target and parent where undefined...
# please post working code
parent = "0123456789"
#position = random.randrange(len(target))
position = random.randrange(len(parent))
child=parent[:position-1]+gene+parent[position+1:]
Now you may want to check your algorithm, since it doesn't perform as
described - but this is another problem !-)
hints:
import string
string.ascii_lowercase
help(random.choice)
astring = "abcd"
alist = list(astring)
alist[0] = 'z'
astring2 = ''.join(alist)
Also note that a function can take arguments:
def fun_with_args(arg1, arg2):
print "in func_with_name : arg1 = %s - arg2 = %s" % (arg1, arg2)
fun_with_args('toto', 'titi')
so you can have the whole algorithm in the function body:
def createChild(parent):
# code here to create child
return child
parent = "0123456789"
child = createChild(parent)
print "parent : %s\nchild : %s" % (parent, child)
> Thanks again - this group is great. I despair of ever being able to
> contribute though :-(
You did. There would be no answer if there were no questions !-)
BTW, may I suggest you to spend some time on a good Python tutorial ?
(there are many good ones freely available on the net).
HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list