Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Python novice

Name: Anonymous 2011-09-29 17:35


class tic:
  turn = "x"
  def changeturn(self, y = turn):
    if y == "x":
      self.turn = "o"
    if y == "o":
      self.turn = "x"

'''WHY THE FUCK ISN'T this working? The changeturn(), I mean, it only works once!'''

Name: Anonymous 2011-10-01 8:17

Today is useful Anonymous day.

>>1
Default parameters are read and evaluated only once, at definition time. As Python defines changeturn, y is bound to the default value of "x", forever.

In order to implement your desired behaviour, do as follows:

class tic(object):
  turn = "x"
  def changeturn(self, y = None):
    if not y:
      y = self.turn
    if y == "x":
      self.turn = "o"
    if y == "o":
      self.turn = "x"


Also, please inherit from Object in your classes: class tic(object).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List