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 ...
随机推荐
- Python 派生类子类继承类
1.创建list类的子类Namedlist,初始化新类,创建新对象实例johnny,检查对象类型,并使用list的一些功能来存储数据 >>> class Namedlist(list ...
- SSL证书在线工具
证书在线工具 如果您是第一次申请SSL证书,如果您对您的服务器如何使用SSL证书还不熟悉的话,我们推荐您使用本套工具,本套工具支持所有SSL服务器证书格式和各种WEB服务器.帮助您在线生成CSR文件, ...
- 把vector中的string对象导入到字符指针数组中
#include <iostream>#include <string>#include <vector>//#include <cctype>#inc ...
- linux命令(11)下查看和添加环境变量
&PATH:决定了shell将到哪些目录中去寻找命令或者程序,PATH值是一系列的目录,当你要运行一个程序时,Linux在这些目录下进行搜寻编译链接. 编辑PATH 声明,其格式为: PATH ...
- JAVA赋值运算符
赋值预算符,简单来说就是把以及定义了值的变量值赋值给刚定义的变量 例如,a学员,与b学员的成绩相同,a学员是80分,我们根据a学员的成绩,输出b学员的成绩. public class Test{ pu ...
- 高可用集群heartbeat全攻略
heartbeat的概念 Linux-HA的全称是High-Availability Linux,它是一个开源项目,这个开源项目的目标是:通过社区开发者的共同努力,提供一个增强linux可靠性(r ...
- 有人向我反馈了一个bug
我是一个前端开发者,但我想这个故事对任何开发者都会引起共鸣的有人向你反馈了一个 bug. “26 楼会议室的灯亮着.它需要被熄灭.”bug 的备注里写道“你应该能在 5 分钟内搞定,只要按一下开关就好 ...
- UILabel使用技巧
UILabel的各种属性与方法的使用(转) #import "LabelTestViewController.h" @implementation LabelTestViewCon ...
- java获取任意日期
现有两个办法 1:Date as = new Date(new Date().getTime()-24*60*60*1000); SimpleDateFormat matter1 = new Sim ...
- 新建的表如果还没有数据,用exp导的时候会忽略
源地址:http://www.07net01.com/2015/07/884873.html