[Tutor] Initialising attributes with random values

Pujo Aji ajikoe at gmail.com
Tue Jul 5 13:54:14 CEST 2005


use : 

random.randint(1,5)

so complete code will be:

class something:
    def __init__(self, name):
        self.name = name
        self.size = random.randint(1,5)
        self.strength = random.randint(1,5)



pujo

On 7/5/05, Max Russell <max_russell2000 at yahoo.co.uk> wrote:
> Hello there;
> 
> If I have a class and want to initalise it, with
> random values assigned to the attributes, what is the
> best way of doing this?
> 
> For example:
> 
>     def __init__(self, name, size = 0, strength = 0):
>         self.name = name
>         self.size = size
>         self.strength = strength
> 
> starts with values of 0. But how can I neatly say,
> start with a random value from (for example) 1 - 5?
> 
> thanks
> Max
> 
> 
> 
> ___________________________________________________________
> How much free photo storage do you get? Store your holiday
> snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list