Re: calling functions



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@xxxxxxxxxxx'.split('@')])"
.



Relevant Pages

  • Re: Waiter, theres a noob in my soup!
    ... Thanks Gene, but I don't know which end is up with Python. ... Anybody have a Ruby example of grabbing html pages from a site map full ... that site and request each page and grab things from it. ...
    (comp.lang.ruby)
  • Re: Size of Y Chromosome
    ... If Chromosome don't get partner to recombine than there is tendency ... If gene deletion by natural selection if it is correct why there is ... chances that the organism with the mutated B gene surviving 99 ... eventually gene B will mutate. ...
    (sci.bio.evolution)
  • Python-URL! - weekly Python news and links (Sep 3)
    ... the gene that coded for it would quickly get weeded ... The first Python 3000 release is out -- Python 3.0a1. ... mailing lists ... Suggestions/corrections for next week's posting are always welcome. ...
    (comp.lang.python)
  • Python-URL! - weekly Python news and links (Sep 3)
    ... the gene that coded for it would quickly get weeded ... The first Python 3000 release is out -- Python 3.0a1. ... mailing lists ... Suggestions/corrections for next week's posting are always welcome. ...
    (comp.lang.python.announce)
  • Re: Macroevolution Mechanism Demonstrated
    ... have two sets of chromosomes and that in order to get big changes, ... mutations in the same gene in two chromosomes in a single organism. ... so you only need to mutate one of the ...
    (talk.origins)