user3245282
user3245282

Reputation: 11

trouble with Boolean testing the name of a class attribute in python

I have a class called GamePiece, and it has an attribute image that is either a file names "blue.jpg" or "green.jpg". I want my program to do different things depending on which file has been assigned to an instance of this object, myPiece.

E.g., if myPiece.image is "blue.jpg" or myPiece.image is "green.jpg". EXACTLY how do I check this? I'm probably doing something stupid with syntax, but I'm not having any luck doing this.

Upvotes: 0

Views: 55

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1125078

Just use == to test for equality:

if myPiece.image == 'blue.jpg':
    # blue
else:
    # green

This assumes that image is a string value with just the filename.

Upvotes: 1

Related Questions