博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python __str__ __lt__常用方法案例(创建一个类People 包含属性name, city 可以转换为字符串形式(__str__) 包含方法moveto )
阅读量:3933 次
发布时间:2019-05-23

本文共 391 字,大约阅读时间需要 1 分钟。

要求:

创建一个类People

包含属性name, city
可以转换为字符串形式(__str__
包含方法moveto (self,newcity)用新的城市去替代之前的城市
可以按照city排序 (__lt__) 创建4个人对象
放到列表进行排序

class People():    def __init__(self,name,city):        self.name=name        self.city=city    def __str__(self):        return'<%s:%s>'%(self.name,self.city)    def moveto(self,newcity):        self.city=newcity    def __lt__(self,other):        return self.city

在这里插入图片描述

转载地址:http://cyhgn.baihongyu.com/

你可能感兴趣的文章
Lesson 3.1 - Python Core Data Types
查看>>
Lesson3.2 variables
查看>>
3.4.2 - Operators & 3.4.3 division and truncation
查看>>
3.6 - Maya Commands: setAttr
查看>>
3.7.1 - Strings
查看>>
3.7.4 - Indexing and Slicing Strings
查看>>
3.7.5 - Modifying Strings
查看>>
3.7.6 - String Methods
查看>>
3.8 - Using the Print Function
查看>>
3.9.1 - Lists in Python
查看>>
3.9.2 - Lists - Adding and Removing Objects
查看>>
3.9.3 - Sorting Lists
查看>>
3.10 - Maya Commands: ls
查看>>
3.11 - Dictionaries in Python
查看>>
3.12 - Tuples in Python
查看>>
4.4 - For Loops
查看>>
4.2.2 - Logical and/or Operators
查看>>
Lesson 4 Part 2 Softmax Regression
查看>>
文章中运用到的数学公式
查看>>
Projective Dynamics: Fusing Constraint Projections for Fast Simulation
查看>>