[Tutor] Random Variable at root
Jonathon Sisson
sisson.j at gmail.com
Tue Oct 3 22:16:52 CEST 2006
Hugo,
You need to create an instance of the Root class before you can call
rootState in your final print statement. Your code doesn't do this. A
class is merely a template, something like blueprints to a house. For
you to be able to unlock the front door (for instance), you need to
actually build a specific "instance" of the house first. "313 Hillcrest
Lane" and "4214 Willowbrook Drive" might be two houses that have the
same blueprint, but maintain different states from each other because
they are different "instances" of class House.
To make this work, remove the final print statement and replace it with
this:
# instantiates ("builds") a copy of the Root class and names it "root"
root = Root(0)
print "The character state at the root is %0.f" % root._rootState
Note the change to the end of the print statement. Your code has
rootState, mine has root._rootState (the specific instance of Root,
followed by the "dot" operator, followed by the attribute or method you
are trying to access). I'm a bit confused as to whether you intended
_rootState to be a copy of the *original* value passed in, with
rootState maintaining a copy of the *current* value, or if that is a
typo. Please elaborate.
Hope this helps clear up confusion (as opposed to creating more...heh).
Jonathon
halamillo wrote:
> Hello,
> I'm a really-green-to-python Biology Grad Student. I;m starting this
> code so I can assign either a 0 or a 1 at a root node that I will
> later evolve throughout a tree, but for some reason it is not printing
> the rootState value. I know its probably a really stupid mistake, but
> I can't seem to work it out. Can anyone give me insight why? Thanks.
>
> import random
> from random import random as rnd
> from math import exp
>
>
> class Root:
> """Single Node in a Tree"""
>
> def __init__( self, rootState ):
>
>
> self._rootState = rootState
>
> # random choice for root state from a list
> for i in range(1):
> rootState = random.choice([0, 1])
>
>
> def __str__( self ):
> """Root string representation"""
>
>
> return str( self._rootState )
> print "The character state at the root is %0.f" % rootState
>
>
> Hugo Alamillo
> Biological Sciences
> 265 Eastlick
> PO Box 644236
> Washington State University
> Pullman, WA 99164
>
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list