你能检查一个类中变量的多个值吗?

时间:2017-07-21 23:18:07

标签: python python-3.x class

我正在制作国际象棋程序,我想知道我是否可以通过检查bpawn1 - bpawn8的x位置和y位置并查看x和y位置是否与白色pawn相同来更轻松地完成一个过程所以它杀了典当。

<a href="#" id="status" data-type="select" data-pk="1" data-title="Select status"></a>    
<script>
  $(function() {

    $("#status").editable({
      type: "select",
      title: 'Select Fruit',
      source: [
        {text : "Apple", value: "option_1"},
        {text : "Orange", value: "option_2"},
        {text : "Mango",value: "option_3"},
        {text : "Strawberry",value: "option_4"}
      ],
      display: function(value, sourceData) {

        if (value) { // value = "option_3" etc.
          $(this).html(value);
        }

        /* OR if you want to access the selected source object  ...

        var selected = $.fn.editableutils.itemsByValue(value, sourceData); 
        if (selected.length) {
           $(this).html(selected[0].value);
        }  */
      }
    });
  });
</script>

1 个答案:

答案 0 :(得分:0)

这会是你想要的吗?

In [15]: pawn_pos = (1, 3)

In [16]: rook_pos = (4, 6)

In [17]: pawn_pos == rook_pos
Out[17]: False

In [18]: rook_pos = (1, 3)

In [19]: pawn_pos == rook_pos
Out[19]: True

或者,您可以覆盖__eq__方法。

In [26]: class ChessPiece:
    ...:     def __init__(self, color, x, y):
    ...:         self.x = x
    ...:         self.y = y
    ...:         self.color = color
    ...:     def __eq__(self, other):
    ...:         return (self.x, self.y) == (other.x, other.y)
    ...:     

In [27]: c1 = ChessPiece('black', 1, 3)

In [28]: c2 = ChessPiece('white', 1, 4)

In [29]: c1 == c2
Out[29]: False

In [30]: c2 = ChessPiece('white', 1, 3)

In [31]: c1 == c2
Out[31]: True