Skip to main content
 首页 » 编程设计

python之python : how 中的类型转换类

2025年05月04日16TianFang

在这里,我尝试将社交媒体个人资料模拟为类“个人资料”,其中您有姓名、一群 friend 以及添加和删除 friend 的能力。我想创建一个方法,调用该方法时,将按字母顺序打印好友列表。

问题:我收到一条警告,提示我无法对无法排序的类型进行排序。 Python 将我的实例变量视为“配置文件对象”,而不是我可以排序和打印的列表。

这是我的代码:

class Profile(object): 
    """ 
    Represent a person's social profile 
 
    Argument: 
    name (string): a person's name - assumed to uniquely identify a person 
 
    Attributes: 
    name (string): a person's name - assumed to uniquely identify a person 
    statuses (list): a list containing a person's statuses - initialized to [] 
    friends (set): set of friends for the given person. 
                   it is the set of profile objects representing these friends. 
    """ 
 
    def __init__(self, name): 
        self.name = name 
        self.friends = set() 
        self.statuses = [] 
 
    def __str__(self): 
        return self.name + " is " + self.get_last_status() 
 
    def update_status(self, status): 
        self.statuses.append(status) 
        return self 
 
    def get_last_status(self): 
        if len(self.statuses) == 0: 
            return "None" 
        else: 
            return self.statuses[-1] 
 
    def add_friend(self, friend_profile): 
        self.friends.add(friend_profile) 
        friend_profile.friends.add(self) 
        return self 
 
    def get_friends(self): 
        if len(self.friends) == 0: 
            return "None" 
        else: 
            friends_lst = list(self.friends) 
            return sorted(friends_lst) 

在我填写 friend 列表(来自测试模块)并调用 get_friends 方法后,python 告诉我:

 File "/home/tjm/Documents/CS021/social.py", line 84, in get_friends 
    return sorted(friends_lst) 
TypeError: unorderable types: Profile() < Profile() 

为什么我不能简单地将对象类型转换为列表形式?我应该怎么做才能让 get_friends 返回按字母顺序排序的 friend 列表?

请您参考如下方法:

排序算法查找是否存在 __eq__、__ne__、__lt__、__le__、__gt__ ,__ge__ 类定义中的方法来比较从它们创建的实例。您需要覆盖这些方法以调整它们的行为。

出于性能原因,我建议您为您的类定义一些 integer 属性,例如 id 并将其用于比较而不是 name 有字符串比较开销。

class Profile(object): 
    def __eq__(self, profile): 
        return self.id == profile.id # I made it up the id property. 
 
    def __lt__(self, profile): 
        return self.id < profile.id 
 
    def __hash__(self): 
        return hash(self.id) 
 
    ... 

或者,如果您不想打扰自己重写这些方法,您可以传递一个 key 函数来排序算法:

>>> friend_list = [<Profile: id=120>, <Profile: id=121>, <Profile: id=115>] 
>>> friend_list.sort(key=lambda p: p.id, reverse=True) 

使用operator.attrgetter;

>>> import operator 
>>> new_friend_list = sorted(friend_list, key=operator.attrgetter('id'))