简体   繁体   English

我忘记了基本的 python 技能我做错了什么?

[英]I forgot my basic python skills What am I doing wrong?

I'm little forgotten about python sdince I don't use it since a while and I can't solve this seemingly very basic error.我几乎忘记了 python sdince 我有一段时间没有使用它,我无法解决这个看似非常基本的错误。

I'm doing a very naif approach to code a chess engine and I'm not being able of correctly separate instances of classes.我正在使用一种非常天真的方法来编写国际象棋引擎,但我无法正确分离类的实例。

In my code:在我的代码中:

class Piece:
  def __init__(self, name='', symbol='', color='', value=0, position=(0,0)):
   self.name=name   
   self.color=color
...
class Army:
  def __init__(self, color="", pieces=[]):
      self.color=color
      self.pieces=pieces
black=Army()
for rank in range(8):
 black.pieces.append(Piece())
 black.pieces[rank].color="B"
 print(Army().pieces[rank].color)

Output: Output:

B
B
B
B
B
B
B
B

Instead of '' to which Piece() defaults.而不是 Piece() 默认的 '' 。 Note that the output points to Army() and not to black instance for which that output would be the expected.请注意,output 指向 Army() 而不是黑色实例,而 output 将是预期的。

Shouldn't separate class instances take separate values?单独的 class 实例不应该采用单独的值吗? I really can't figure out what's going on.我真的不知道发生了什么事。

My full code is:我的完整代码是:

class Game:
 def __init__(self, name="chess",score=0,board=""):
  self.name=name
  self.board=board
  self.score=score
class Board:
 def __init__(self, position="", size=[8,8]):
   self.size=size
   self.position=position
class Piece:
  def __init__(self, name='', symbol='', color='', value=0, position=(0,0)):
# vector=[[0,0],[0,0]])
   self.name=name
   self.color=color
   self.value=value
   self.position=position
#   self.vector=vector

class Army:
  def __init__(self, color="", pieces=[]):
      self.color=color
      self.pieces=pieces
chess=Game()
chess.name="FIDE"
chess.board=Board()
chess.board.size=[8,8]
checker=Piece()
checker.name="checker"
checker.value=100
checker.vector=(1,0)
black=Army()
black.color="B"
for rank in range(8):
 black.pieces.append(Piece())
 black.pieces[rank].color="B"
print(Army().pieces)

white=Army()
white.color="W"
for rank in range(8):
 white.pieces.append(Piece())
 white.pieces[rank].color="W"
 print( len(black.pieces))
for ch in white.pieces:
 print (ch.color)
print(black)
print(white)
print(len(white.pieces))
print(black.color)
#print (white.pieces.color)

I suppose that's because you should not use mutable lists as a default values.我想那是因为你不应该使用可变列表作为默认值。 All the instances of Board() keep references to the same size list. Board()的所有实例都保持对相同size列表的引用。 All the Army() instances use the same pieces list as well.所有Army()实例也使用相同的pieces列表。

Please try to update your code using approach described below:请尝试使用以下描述的方法更新您的代码:

def __init__(self, color="", pieces=[]):
    self.pieces = pieces

# ->

def __init__(self, color="", pieces=None):
    if pieces is None:
        pieces = []
    self.pieces = pieces

So you'll be creating a new, separate, instance of list per each __init__() call.因此,您将为每个__init__()调用创建一个新的、单独的列表实例。

More info: Mutable Default Method Arguments In Python更多信息: 可变默认方法 Arguments 在 Python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM