3

So, I'm getting a really weird error. For some reason python is not importing random as the module, and seems to me to be importing it as a function (as seen here). Why is it doing this? Using import random and random.choice() works fine in other modules. Why not here?

update: Turns out I was defining random again in logic.py. I had forgot I did this since I wasn't using it anymore. Commented it out and it works fine.

Traceback:

Traceback (most recent call last):
  File "H:\workspace37\RockPaperScissors\irps\driver.py", line 20, in <module>
    print "%r" % dtree.root.get_value(history)
  File "H:\workspace37\RockPaperScissors\irps\datastructures.py", line 69, in get_value
    return self.operation(self.left.get_value(history), self.right.get_value(history))
  File "H:\workspace37\RockPaperScissors\irps\datastructures.py", line 69, in get_value
    return self.operation(self.left.get_value(history), self.right.get_value(history))
  File "H:\workspace37\RockPaperScissors\irps\logic.py", line 53, in either
    return random.choice((a, b))
AttributeError: 'function' object has no attribute 'choice'

I've tried to include the relevant bits of code.

logic.py:

import random 
#from random import choice # works if I change random.choice(...) to just choice(...)

ROCK = 'R'
PAPER = 'P'
SCISSORS = 'S'
RPS_TUPLE = (ROCK, PAPER, SCISSORS)
RPS_SET = set((ROCK, PAPER, SCISSORS))
PLAYER = 0
OPPONENT = 1
PLAYERS = (PLAYER, OPPONENT)

def assert_in_set(a, b):
    assert a in RPS_SET
    assert b in RPS_SET

def neither(a, b):
    assert_in_set(a, b)
    diff = RPS_SET.difference((a, b))
    print "diff = ", diff
    return random.choice(tuple(diff))

def either(a, b):
    assert_in_set(a, b)
    return random.choice((a, b)) #line 53

#EDITED TO INCLUDE THESE LINES FURTHER DOWN
def random(a, b):
    return random.choice(RPS_TUPLE)

ALL_OPERATORS = (neither, either)

datastructures.py

from collections import deque
from logic import RPS_TUPLE, ALL_OPERATORS, PLAYERS
import random
import sys

class OperationNode(TreeNode):

    def __init__(self, parent, left, right, operation):
        super(OperationNode, self).__init__(parent, left, right)
        self.operation = operation

    def get_value(self, history):
        return self.operation(self.left.get_value(history), self.right.get_value(history)) # line 69


class TerminalNode(TreeNode):

    def __init__(self, parent, player, position):
        super(TerminalNode, self).__init__(parent, None, None)
        self.player = player
        self.position = position

    def get_value(self, history):
        # history is a deque of tuples
        return history[self.position][self.player]
4
  • 2
    Did you define random function in logic.py ? Commented Oct 30, 2013 at 4:45
  • 1
    Have you saved the file correctly before rerunning it? Have you got from random import random anywhere? Commented Oct 30, 2013 at 4:46
  • 3
    Try printing random.__name__ right before the attribute error. That would at least tell you which function it thinks random is which might lead you to the actual problem elsewhere... Commented Oct 30, 2013 at 4:50
  • 1
    @falsetru random was defined later in the logic.py file. I had forgot I had done that since I wasn't using it anymore. This was the problem. edit: If you want to post this as an answer I'll accept it. Commented Oct 30, 2013 at 19:02

1 Answer 1

1

Did you define random function in logic.py?

That could be a cause of a problem.

>>> def random():
...     pass
...
>>> random.choice
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'choice'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.