Python - 打印和类

时间:2016-08-08 01:57:21

标签: python python-2.7 oop methods printing

我在__str____repr__上阅读了比健康更多的帖子和文档,咨询了各种文本,仍然无法解决此打印问题,因此我将其排除在外那里。

以下是我想测试的功能。代码不是我的,但我想知道它是如何工作的。我需要看到一个人性化的结果打印输出(即没有十六进制),以便我可以玩它,一般来说,好吧,学习一些东西,因为我真的不知道它是如何做它的&#39做的。

def get_ordered_adoption_center_list(adopter, list_of_adoption_centers):
    """
    The method returns a list of an organized adoption_center such that the scores for each AdoptionCenter to the Adopter will be ordered from highest score to lowest score.
    """
    list_of_adoption_centers.sort(key=lambda center:center.name)
    list_of_adoption_centers.sort(key=lambda center:adopter.get_score(center), reverse=True)
    return list_of_adoption_centers

下面是它所使用的相关代码横截面,我写过。

import random
import string

class AdoptionCenter:
    """
    The AdoptionCenter class stores the important information that a
    client would need to know about, such as the different numbers of
    species stored, the location, and the name. It also has a method to adopt a pet.
    """
    def __init__(self, name, species_types, location):
        self.name = name
        self.species_types = species_types
        self.location = (float(location[0]), float(location[1]))
    def get_number_of_species(self, species):
        return self.species_types.get(species, 0)
    def get_location(self):
        return self.location
    def get_species_count(self):
        return self.species_types.copy()
    def get_name(self):
        return self.name
    def adopt_pet(self, species):
        self.species_types[species] = self.species_types[species] - 1
        if self.species_types[species] <= 0:
            del self.species_types[species]
    def __str__(self):
        return "%s" % (self.name)



class Adopter:
    """ 
    Adopters represent people interested in adopting a species.
    They have a desired species type that they want, and their score is
    simply the number of species that the shelter has of that species.
    """
    def __init__(self, name, desired_species):
        self.name = name
        self.desired_species = desired_species
    def get_name(self):
        return self.name 
    def get_desired_species(self):
        return self.desired_species
    def get_score(self, adoption_center):
        num_desired = adoption_center.get_number_of_species(self.desired_species)
        score = float(1 * num_desired)
        return score
    def __str__(self):
        return "%s and score is %d" % (self.name, self.get_score)


class FlexibleAdopter(Adopter):
    """
    A FlexibleAdopter still has one type of species that they desire,
    but they are also alright with considering other types of species.
    considered_species is a list containing the other species the adopter will consider
    Their score should be 1x their desired species + .3x all of their desired species
    """
    def __init__(self, name, desired_species, considered_species):
        Adopter.__init__(self, name, desired_species)
        self.considered_species = considered_species
    def get_score(self, adoption_center):
        num_Other = 0
        for animal in self.considered_species:
            if adoption_center.get_number_of_species(animal) > 0:
                num_Other += adoption_center.get_number_of_species(animal)
        adopter_score = Adopter.get_score(self, adoption_center)
        score = adopter_score + 0.3 * num_Other
        return score
    def __str__(self):
        return "%s and score is %d" % (self.name, self.get_score)


class FearfulAdopter(Adopter):
    """
    A FearfulAdopter is afraid of a particular species of animal.
    If the adoption center has one or more of those animals in it, they will
    be a bit more reluctant to go there due to the presence of the feared species.
    Their score should be 1x number of desired species - .3x the number of feared species
    """
    def __init__(self, name, desired_species, feared_species):
        Adopter.__init__(self, name, desired_species)
        self.feared_species = feared_species
    def get_score(self, adoption_center):
        num_feared = adoption_center.get_number_of_species(self.feared_species)
        adopter_score = Adopter.get_score(self, adoption_center)
        score = adopter_score - (0.3 * num_feared)
        return max(0.0, score)
    def __str__(self):
        return "%s and score is %d" % (self.name, self.get_score)


class AllergicAdopter(Adopter):
    """
    An AllergicAdopter is extremely allergic to a one or more species and cannot
    even be around it a little bit! If the adoption center contains one or more of
    these animals, they will not go there.
    Score should be 0 if the center contains any of the animals, or 1x number of desired animals if not
    """
    def __init__(self, name, desired_species, allergic_species):
        Adopter.__init__(self, name, desired_species)
        self.allergic_species = allergic_species
    def get_score(self, adoption_center):
        for animal in self.allergic_species:
            if animal in adoption_center.get_species_count().keys():
                return 0.0
        return 1.0 * adoption_center.get_number_of_species(self.desired_species)
    def __str__(self):
        return "%s and score is %d" % (self.name, self.get_score)

我尝试在各个类中放置__str____repr__方法。我所读到的建议__str__就是我所追求的。我也尝试过一个简单的&#34; for循环&#34;在函数体本身中使用print语句。后一种方法导致屏幕充满错误。我在成功之前使用__str__进行了打印,但是这一切似乎都让我失望了。任何见解将不胜感激。

1 个答案:

答案 0 :(得分:1)

根据您的评论,您将在列表中打印这些内容。 print函数检索由__str__定义的要打印的每个参数的字符串表示形式。列表的字符串表示由方括号组成,括起逗号+以空格分隔的每个项目repr表示的序列,由__repr__定义。

>>> class A:
...     def __str__(self):
...             return 's'
...     def __repr__(self):
...             return 'r'
...
>>> l = [A(), A()]
>>> print(l)
[r, r]
>>> print(*l)
s s

如果要查看每个项目的人类可读字符串表示形式,请使用print解压缩运算符将它们直接传递给*函数,或者循环遍历该列表并调用{{1每个项目。如果您希望在直接打印包含它们的列表时看到每个项目的人类可读字符串表示,您可以定义print返回与__repr__方法相同的内容,但它是不推荐,因为这应该是用__str__再现对象的方式。