Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在同一个类中存储类的其他对象的引用?_Python_Class_Oop_Object_Object Reference - Fatal编程技术网

Python 如何在同一个类中存储类的其他对象的引用?

Python 如何在同一个类中存储类的其他对象的引用?,python,class,oop,object,object-reference,Python,Class,Oop,Object,Object Reference,我有上课的经验,但我有点困惑。 问题是,它说将类的其他对象的引用也存储在类对象中。 我把我写的代码放在下面。请指导我如何解决这个问题? 属性朋友属于一个人,每个人的属性都不同 这意味着friends应该是实例变量而不是类变量 class Person: def __init__(self, first_name, last_name): self.first_name = first_name self.last_name = last_name s

我有上课的经验,但我有点困惑。 问题是,它说将类的其他对象的引用也存储在类对象中。 我把我写的代码放在下面。请指导我如何解决这个问题?

属性朋友属于一个人,每个人的属性都不同

这意味着friends应该是实例变量而不是类变量

class Person: 

  def __init__(self, first_name, last_name):
      self.first_name = first_name
      self.last_name = last_name
      self.friends = []  # person can have a list of friends

  def add_friend(self, friend):
      " Add a friend for this person "
      self.friends.append(friend)

  def get_name(self):
    return self.first_name + " " + self.last_name

  def get_friends(self):
    " List of friends as a string "
    return self.friends  # returns list of friends

  def __str__(self):
    """ Converts attributes of person to string 
        (useful for print) """
    return self.get_name()
示例用法

输出


有什么问题吗?@juanpa.arrivillaga非常感谢你注意到这个错误,我显然忘记了那个错误。然而,真正让我困惑的是,存储类u的其他对象的引用的最佳方法是什么,可以从图像中得到更清晰的解释question@HarisHarris-添加了一个示例用法。这是否有助于澄清如何将本例中的其他对象朋友添加到person实例中?“其他人将添加到self.friends列表中。@DarryIG首先,我将道歉标记为错误的人。”。我是新来的,正在努力适应这项农活。其次是的,现在很清楚了。非常感谢。@HarisHarris如果您有任何后续问题,请随时提问。@HarisHarris不要忘记-
class Person: 

  def __init__(self, first_name, last_name):
      self.first_name = first_name
      self.last_name = last_name
      self.friends = []  # person can have a list of friends

  def add_friend(self, friend):
      " Add a friend for this person "
      self.friends.append(friend)

  def get_name(self):
    return self.first_name + " " + self.last_name

  def get_friends(self):
    " List of friends as a string "
    return self.friends  # returns list of friends

  def __str__(self):
    """ Converts attributes of person to string 
        (useful for print) """
    return self.get_name()
# Example
p = Person('Bob', "Johnson")

# Add some friends (uses Person constructor to create persons)
p.add_friend(Person("Mary", "Sullivan"))  
p.add_friend(Person("Jodie", "Oliver"))
p.add_friend(Person("Danny", "Washington"))

# Show p's friends
print(f'Person: {p}')   # uses __str__ Person method to convert p to string
                        # for printing
print('Friends')
for friend in p.get_friends():
  print('\t', friend)  # since friend is a Person, Person method __str__
                       # knows how to convert to string for printing
Person: Bob Johnson
Friends
     Mary Sullivan
     Jodie Oliver
     Danny Washington