0

I would be grateful for your advice on this.

So need to implement a function to find out whether a chess piece is at a specific coordinate on a chessboard:

The piece is defined by the class Piece and a subclass Bishop, Rook, etc.

Co-ordinates & side by a tuple (col, row, side) and the side represented by a bool (True for white, False for Black)

so assigned an instance variable:

wb1 = Bishop (1,1,True)

If I try to print wb1, returns the memory object

<__main__.Bishop object at 0x7fdd440094f0>

Is there anyway, I can get it to print wb1 - the self variable, rather the memory object.

Many thanks

Alexander
  • 59,041
  • 12
  • 98
  • 151
Agyamarky
  • 61
  • 2
  • 1
    python uses default string representations for objects. if they're not intentionally defined for a class printing objects may be useless. try this: ```print(dir(wb1))``` to see what attributes the Bishop has, then print those things, which may look like ```print(f'{wb1.row}-{wb1.col}-{wb.color}')``` (this example using made-up attributes) – AbbeGijly Dec 10 '21 at 22:45
  • 5
    The term "memory object" is something you just made up. It has no real meaning. You need to override the `__repr__` method – Mad Physicist Dec 10 '21 at 22:45
  • By your understanding, what's a non-memory object? – Alexander Dec 10 '21 at 22:59
  • 1
    @Alexander It appears that he's using the term for a class whose repr just shows its memory address instead of something nice. – Barmar Dec 10 '21 at 23:04
  • "f I try to print wb1, returns the memory object" it doesn't *return anything*, it *prints* the string `<__main__.Bishop object at 0x7fdd440094f0>` to standard output. That is the default representation of an object. Why were you *expecting anything else?* – juanpa.arrivillaga Dec 10 '21 at 23:05
  • Thanks for your comments. Sorry new to Py, so apologies if I get the terminology wrong. Basically, wb1 is the White Bishop piece at cord 1,1. If I needed to find out what piece was at coordinate 1,1 - I'd like it to print out wb1 rather than the memory address if that makes sense..so if I had a func like find_piece(1,1) it would return wb1 – Agyamarky Dec 10 '21 at 23:07
  • @Agyamarky I'm sorry, but that doesn't really clarify anything. What, **exactly** do you expect `print(wb1)` to print? It isn't clear – juanpa.arrivillaga Dec 10 '21 at 23:18
  • the string 'wb1' and not <__main__.Bishop object at 0x7fdd440094f0> – Agyamarky Dec 10 '21 at 23:22
  • 1
    @Agyamarky why would it print the string `wb1`? That doesn't make any sense. That's the name of the variable you assigned the object to. The object has *no knowledge* of that name, and moreover, it *shouldn't*. Variables are *source code*, they are for the *programmer*, they shouldn't contain data. – juanpa.arrivillaga Dec 10 '21 at 23:25
  • 1
    @Agyamarky To add onto what juanpa said: variable names are meaningless to the computer. They exist for the convenience of the computer. If you would like to make an object be associated with a string like `wb1`, then you need to make that string *part* of the object. Alternatively, you can use a dictionary to key objects by some string value of your chosing – Alexander Dec 10 '21 at 23:28
  • many thanks guys, will try a dictionary - think that way it might work.. – Agyamarky Dec 10 '21 at 23:35

0 Answers0