Class attributes
In order to print Card objects in a way that people can easily read, we need a mapping from the integer codes to the corresponding ranks and suits. A natural way to do that is with lists of strings.
class Card:
""" represents a standard playing card.
class attributes: suit_names, rank_names
instance attributes: suit, rank """ suit_names = ['Clubs','Diamonds','Hearts','Spades']
rank_names = [None,'Ace','','','','','','',
'','','','Jack','Queen','King'] def __init__(self, suit=0, rank=2):
self.suit = suit
self.rank = rank def __str__(self):
return '%s of %s' % (Card.suit_names[self.suit],
Card.rank_names[self.rank])
Because suit_names and rank_names are defined outside of any method, they are class attributes; that is, they are associated with the class Card rather than with a particular Card instance. Attributes like suit and rank are more precisely called instance attributes because they are associated with a particular instance. Both kinds of attributes are accessed using dot notation. For example, in __str__, self is a Card object, and self.rank is its rank. Similarly, Card is a class object, and Card.rank_names is a list of strings associated with the class. Every card has its own suit and rank, but there is only one copy of suit_names and rank_names.
Finally, the expression Card.rank_names[self.rank] means ‘use the attribute rank from the object self as an index into the list rank_names from the class Card, and select the appropriate string’.
The first element of rank_names is None because there is no card with rank zero. By including None as a place-keeper, we get a mapping with the nice property that index 2 maps to the string ‘2’, and so on.
Here is a diagram that shows the Card class object and one Card instance:
from Thinking in Python
Class attributes的更多相关文章
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- 执行打的maven jar包时出现“Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes”
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for ...
- webapi filter过滤器中获得请求的方法详情(方法名,Attributes)
public class GlobalActionFilter : ActionFilterAttribute { private string _requestId; public override ...
- .NET Attributes
Attributes 特性 公共语言运行时允许添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法和属性等.Attributes和Microso ...
- 给iOS开发新手送点福利,简述文本属性Attributes的用法
给iOS开发新手送点福利,简述文本属性Attributes的用法 文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSF ...
- jQuery in action 3rd - Working with properties, attributes, and data
properties properties 是 JavaScript 对象内在的属性,可以进行动态创建,修改等操作. attributes 指的是 DOM 元素标记出来的属性,不是实例对象的属性. 例 ...
- Unsupported configuration attributes: [FILE_UPLOAD]
Caused by: java.lang.IllegalArgumentException: Unsupported configuration attributes: [FILE_UPLOAD] 情 ...
- Jade模板引擎(一)之Attributes
目录: Attributes Boolean Attributes Style Attributes Class Attributes &Attributes Attributes jade中 ...
- ASP.NET MVC 使用带有短横线的html Attributes(转载)
转载地址:http://www.nmtree.net/2013/10/25/asp-net-mvc-use-dash-in-html-attributes.html 情景再现 我们常常需要一个文本框来 ...
- Create and Use Custom Attributes
http://www.codeproject.com/Articles/1811/Creating-and-Using-Attributes-in-your-NET-applicat Create a ...
随机推荐
- [tty与uart]3.tty驱动分析
转自:http://www.wowotech.net/linux_kenrel/183.html 目录: 1 首先分析设备驱动的注册 1.1 uart_register_driver分析 1.2 tt ...
- CXF超时设置
转自: http://peak.iteye.com/blog/1285211 http://win.sy.blog.163.com/blog/static/9419718620131014385644 ...
- 【MySQL】性能优化 之 延迟关联
[背景] 某业务数据库load 报警异常,cpu usr 达到30-40 ,居高不下.使用工具查看数据库正在执行的sql ,排在前面的大部分是: SELECT id, cu_id, name, in ...
- nginx 出现 13: Permission denied
原文地址:http://www.nginx.cn/695.html 前段时间把程序员的wordpress升级到3.5.1,本身如果没有特别的插件,在后台更新就能完成. 更新完成后在后台发布文章,编辑器 ...
- SOA_环境安装系列5_Oracle ADF安装SOA Extensions(案例)
2015-01-02 Created By BaoXinjian
- Transact-SQL的除法问题
SELECT 3800/365, 3800.0/365; 执行上面的sql,得到的结果是:10, 10.410958 返回优先级较高的参数的数据类型. 有关详细信息,请参阅数据类型优先级 (Trans ...
- $(document).ready() 与 window.onload的区别
1.执行时间 window.onload 必须等到页面内所有元素(包括图片 css js等)加载完毕后才会执行. $(document).ready() 是DOM结构绘制完毕后就执行,不必等到所有元素 ...
- .NET技术+25台服务器怎样支撑世界第54大网站(转)
[编者按]StackOverflow是一个IT技术问答网站,用户可以在网站上 提交和回答问题.当下的StackOverflow已拥有400万个用户,4000万个回答,月PV5.6亿,世界排行第54.然 ...
- [Flex] IFrame系列 —— IFrame嵌入html点击其他组件后页面消失的问题
在flex建的web项目中,打开index.template.html,将param.wmode = "transparent";添加到以下位置 <script type=& ...
- 反人类的MyEclipse之-MyEclipse设置Console字体大小
Windows-->Preference-->General-->Apperence-->Colors and Fonts --> Debug -->Console ...