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 ...
随机推荐
- php常见判断
当要 判断一个变量是否已经声明的时候 可以使用 isset 函数 当要 判断一个变量是否已经赋予数据且不为空 可以用 empty 函数 当要 判断 一个变量 存在且不为空 先isset 函数 再用 e ...
- 黄聪:如何开启IIS7以上的“IIS6管理兼容性”
护卫神PHP套件的安装,需要开启“IIS6管理兼容性”, 那么,如何开启IIS7.IIS7.5.IIS8.0的IIS6兼容模式呢? 设置的时候,请参照如下截图:
- Tomcat插件与Jetty插件在MyEclipse中的配置
-Djetty.port=8101 jetty:run tomcat6:run <plugin> <groupId>org.apache.tomcat.maven</gr ...
- OAF_EO系列6 - Delete详解和实现(案例)
2014-06-14 Created By BaoXinjian
- [实变函数]5.2 非负简单函数的 Lebesgue 积分
1 设 $$\bex \phi(x)=\sum_{i=1}^j c_i\chi_{E_i}(x),\quad c_i\geq 0, \eex$$ 其中 ...
- You need tcl 8.5 or newer in order to run the Redis test
安装Redis时候make test出现的 , 不用下什么tcl8.5再tar了 , 直接yum install tcl就好.
- Linux下SVN服务器安装配置及客户端安装说明
原文地址:http://wenku.baidu.com/link?url=h3dVAMx4azpOXEND5HQEE6nliE8-zc0GSQ03yv4cUs1vXMALXF64UsK7kT7kXm_ ...
- ddl语句
- C++学习44 格式化输出,C++输出格式控制
在输出数据时,为简便起见,往往不指定输出的格式,由系统根据数据的类型采取默认的格式,但有时希望数据按指定的格式输出,如要求以十六进制或八进制形式输出一个 整数,对输出的小数只保留两位小数等.有两种方法 ...
- gc 辅助打印信息
cat xx.xml|grep GC <jvm-arg>-XX:+PrintGCDetails</jvm-arg> <jvm-arg>-XX:+PrintGCDat ...