0

Thanks in advance for any help, I know this is probably a newb question but I just can't find anything via search that seems to answer my question. This is the relevant part of the traceback:

 File "war.py", line 183, in __battle if (card1 > card2): File "war.py", line 24, in __lt__ return self.int_id % 13 < other.int_id % 13 AttributeError: 'function' object has no attribute 'int_id' 

I'm confused about what this means....what is the 'function' object? Also, why is calling lt from a line that's doing a greater than comparison? Why is this a problem now, given that I unit tested my Card class (of which card1 and card2 are instances of), including all rich comparison operators, and it passed?

These are the definition of the rich comparison methods in my Card class (int_id is, as you would probably guess, just an instance variable int):

(sorry the indenting below got messed up)

class Card(): def __init__(self, int_id): self.int_id = int_id def __lt__(self, other): return self.int_id % 13 < other.int_id % 13 def __le__(self, other): return self.int_id % 13 <= other.int_id % 13 def __eq__(self, other): return self.int_id % 13 == other.int_id % 13 def __ne__(self, other): return self.int_id % 13 != other.int_id % 13 def __gt__(self, other): return self.int_id % 13 > other.int_id % 13 def __ge__(self, other): return self.int_id % 13 >= other.int_id % 13 
6
  • Are you sure you defined int_id as a class variable? Commented Aug 9, 2012 at 19:02
  • @Florin, I'm sure I didn't define it as a class variable. As my question states, I intend it to be an instance variable -- it is first referenced and assigned in Card.__init__(), so that each instance of Card has its own int_id. Commented Aug 9, 2012 at 19:05
  • 7
    from the error, it looks like either card1 or card2 is a function and not a Card. That could be because of a simple typo elsewhere, for example card2 = getMyOtherCard instead of card2 = getMyOtherCard() Commented Aug 9, 2012 at 19:06
  • 1
    You should probably include your __init__ code. Commented Aug 9, 2012 at 19:06
  • 1
    @Kevin obviously card1 here is a function because card1>card2 code produces __ lt __ method call Commented Aug 9, 2012 at 19:19

2 Answers 2

5

From the error 'function' object has no attribute 'int_id', I would guess that a function object is being compared against a Card in the line if (card1 > card2):. This might occur if one of the cards was mistakenly assigned as a function. For example, card1 = getMyOtherCard instead of card1 = getMyOtherCard().

Sign up to request clarification or add additional context in comments.

Comments

2

card1 has somehow come to refer to a function (have you left out parentheses on a function or method call?).

As a result, card1 < card2 gets rearranged to card2 < card1 (because function objects don't have comparison operators), which is why __lt__ is being called.

From http://docs.python.org/reference/datamodel.html#object.__lt__:

There are no swapped-argument versions of these methods (to be used when the left argument does not support the operation but the right argument does); rather, __lt__() and __gt__() are each other’s reflection [...]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.