标签:

1、通过实现特殊方法,自定义类型可以表现的跟内置类型一样;

如下代码,实现len, getitem,可使自定义类型表现得如同列表一样。

import collections

from random import choice

Card = collections.namedtuple('Card', ['rank', 'suit'])

class FrenchDeck:

ranks = [str(n) for n in range(2, 11)] + list('JQKA')

suits = 'spades diamonds clubs hearts'.split()

def __init__(self):

self._cards = [Card(rank, suit) for rank in self.ranks

for suit in self.suits]

def __len__(self):

return len(self._cards)

def __getitem__(self, position):

return self._cards[position]

suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)

def spade_hith(card):

rank_value = FrenchDeck.ranks.index(card.rank)

# print(rank_value)

# print(suit_values[card.suit])

return rank_value * len(suit_values) + suit_values[card.suit]

if __name__ == '__main__':

deck = FrenchDeck()

#card = choice(deck)

#print(card)

for card in sorted(deck, key=spade_hith):

print(str(card))

print(repr(card))

2、repr和str

(1) __repr__所返回的字符串应该准确(%r),无歧义,并且尽可能表达出如何用代码创建出这个被创建的对象,如repr(v);输出Vector(3, 4)

(2) __str__在str函数被调用(%s),或者打印一个对象的时候被调用,如str(v);输出(3,4)

(3)默认实现打印对象时,两者没有什么区别

(4)两个特殊方法,只想实现一个时,__repr__是更好的选择,因为如果一个对象没有__str__函数,而python解释器又需要调用时,会用__repr__替代。

两者的区别,参考:https://stackoverflow.com/questions/1436703/difference-between-str-and-repr

标签:

来源: https://www.cnblogs.com/573177885qq/p/9828645.html

python的数据模型_(一)python 数据模型相关推荐

  1. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  2. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  3. python集群_使用Python集群文档

    python集群 Natural Language Processing has made huge advancements in the last years. Currently, variou ...

  4. python 网页编程_通过Python编程检索网页

    python 网页编程 The internet and the World Wide Web (WWW), is probably the most prominent source of info ...

  5. python机器学习预测_使用Python和机器学习预测未来的股市趋势

    python机器学习预测 Note from Towards Data Science's editors: While we allow independent authors to publish ...

  6. python高斯求和_利用Python进行数据分析(3)- 列表、元组、字典、集合

    本文主要是对Python的数据结构进行了一个总结,常见的数据结构包含:列表list.元组tuple.字典dict和集合set. image 索引 左边0开始,右边-1开始 通过index()函数查看索 ...

  7. python 免费空间_用python做大数据

    不学Python迟早会被淘汰?Python真有这么好的前景? 最近几年Python编程语言在国内引起不小的轰动,有超越Java之势,本来在美国这个编程语言就是最火的,应用的非常非常的广泛,而Pytho ...

  8. python希腊字母字符串_#10 Python字符串

    前言 通过上一节可知,Python6个序列的内置类型中,最常见的是列表和元组,但在Python中,最常用的数据类型却不是列表和元组,而是字符串.要想深入了解字符串,必须先掌握字符编码问题.因此本篇博文 ...

  9. 使用python预测基金_使用python先知3 1创建预测

    使用python预测基金 This tutorial was created to democratize data science for business users (i.e., minimiz ...

  10. python选择题题目_《Python程序设计》题库 - 选择题

    一.基础知识 1 . Python 语言属于( ) . C A . 机器语言 B . 汇编语言 C .高级语言 D .科学计算语言 2 .下列选项中,不属于 Python 特点的是( ) . B A ...

最新文章

  1. 弹性盒子 flex-direction
  2. 阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第1节 异常_8_Throwable类中3个异常处理的方法...
  3. Java中利用socket实现简单的服务端与客户端的通信(中级)——实现任意双向通信
  4. Paxos Made Simple 中文翻译
  5. Mysql5.7后的password加密和md5
  6. html富文本编辑器插件_vue中使用vuequilleditor富文本编辑器
  7. wxpython控件绑定事件_wxPython事件处理
  8. 宝石光是什么石头_沙漠戈壁的漂亮“石头”让人见了流口水
  9. Struts2文件配置 登陆页面
  10. 面向对象程序设计之封装性、继承性、多态性
  11. 杭电 3400 Line belt 解题报告
  12. Navicat安装教程
  13. WIFI抓包实战篇——使用Kali 同时抓取多个智能家居数据包
  14. 刘汝佳--WERTY
  15. Ubuntu 20.04 双系统安装过程(新手)
  16. 基础篇:6.7)形位公差-基本规则 Basic Rules
  17. 树莓派之BT下载器实现
  18. 一文钱憋死英雄汉!给Unix-Center.Net 的建议
  19. php三行情书,《三行情书》经典语录 经典三行情书 最美三行情书
  20. onenote 插入图片或文件后 显示 您不再有权访问此笔记本。如果还原权限,我们将再次同步

热门文章

  1. 北大软微2021计算机考研难度,2021北京大学软微计算机智能科技方向考研报录情况及备考经验分享...
  2. mysql插入时间少八个小时
  3. 目前最新《Swift4打造今日头条视频实战教程》
  4. 不同计算机用户的区别是什么意思,电脑操作系统的32位和64位分别是什么意思?有什么区别?...
  5. 面向Web服务的游戏设计1:选择适合的Silverlight通信技术
  6. 重庆大学计算机科学与工程学院,何中市
  7. iphone通讯录 android,3个方法,教你如何快速而又有效的将联系人从iPhone转移到安卓...
  8. 怎样才能保证单元测试效果
  9. python 实践--新闻聚合
  10. 【JDK7】新特性(3) JDBC4.1