function - Why is this returning two values? Easy python beginner -
can please explain why enter method in following class returning 2 values? i'm learning python , in process of creating simple game grasp oop , classes. anyhow need enter method return random snippet snippets list. keep getting 2 snippets instead of one. can explain why?
sys import exit random import randint class island(object): def enter(self): pass class loser(island): snippets = ["welcome loser island", "can't win them all", "there's next time"] def enter(self): print loser.snippets[randint(0,len(self.snippets)-1)] loser_test = loser() loser_test.enter()
generally, you'll create class , describe behavior , contents of instance of class. instance of class object type class. example, john = person('john', 'doe')
create person
object, sending 'john'
, 'doe'
object's __init__
method (a constructor).
the following sticks instances of class making use of word self
. self
not keyword (like in
); it's word object's description uses refer object itself. can use word (like xyz
in for xyz in [1,2,3]
), self
preferred.
>>> import random >>> class l(object): ... def __init__(self): ... self.snippets = ["welcome", "can't", "there's"] ... def enter(self): ... print (random.choice(self.snippets)) ... >>> l = l() >>> l.enter() there's >>> l.enter() there's >>> l.enter() welcome >>> l.enter() can't >>>