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的更多相关文章

  1. Eclipse "Unable to install breakpoint due to missing line number attributes..."

    Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...

  2. 执行打的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 ...

  3. webapi filter过滤器中获得请求的方法详情(方法名,Attributes)

    public class GlobalActionFilter : ActionFilterAttribute { private string _requestId; public override ...

  4. .NET Attributes

    Attributes 特性      公共语言运行时允许添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法和属性等.Attributes和Microso ...

  5. 给iOS开发新手送点福利,简述文本属性Attributes的用法

    给iOS开发新手送点福利,简述文本属性Attributes的用法   文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSF ...

  6. jQuery in action 3rd - Working with properties, attributes, and data

    properties properties 是 JavaScript 对象内在的属性,可以进行动态创建,修改等操作. attributes 指的是 DOM 元素标记出来的属性,不是实例对象的属性. 例 ...

  7. Unsupported configuration attributes: [FILE_UPLOAD]

    Caused by: java.lang.IllegalArgumentException: Unsupported configuration attributes: [FILE_UPLOAD] 情 ...

  8. Jade模板引擎(一)之Attributes

    目录: Attributes Boolean Attributes Style Attributes Class Attributes &Attributes Attributes jade中 ...

  9. ASP.NET MVC 使用带有短横线的html Attributes(转载)

    转载地址:http://www.nmtree.net/2013/10/25/asp-net-mvc-use-dash-in-html-attributes.html 情景再现 我们常常需要一个文本框来 ...

  10. Create and Use Custom Attributes

    http://www.codeproject.com/Articles/1811/Creating-and-Using-Attributes-in-your-NET-applicat Create a ...

随机推荐

  1. 战胜忧虑<4>——让平均概率来替你分忧

    让平均概率来替你分忧. 我们可以根据事情发生的平均率来评估我们的忧虑究竟值不值,如此一来,我想你和我应该可以去除99%的忧虑. 故事 我从小生长在密苏里州的一个农场,有一天,正帮妈妈采摘樱桃的时候,我 ...

  2. 如何缩短SQL Server 的启动时间

    将/nosplash添加到SQLServer Manageement Studio的快捷方式可以缩短启动时间.为此,右击SSMS快捷方式(位于你的桌面上.start菜单中或任务栏中)并选择Proper ...

  3. scala高级内容(一) Case Class

    一. 操作符 自定义操作符 操作付默认左结合调用.除了以:结尾的操作符是右结合调用 object OperaterTest extends App{ val a: myInt = new myInt( ...

  4. Maven打包web工程成WAR

    其实不一定要通过Goals:package来打war包,直接run as maven bulid也行:

  5. [实变函数]2.1 度量空间 (metric space), $n$ 维 Euclidean 空间

    1 回忆:    $$\bex    \lim_{n\to\infty}a_n=a\lra \forall\ \ve>0,\ \exists\ N,\ \forall\ n\geq N,\mbo ...

  6. gridview--基本的gridview

    GridView 元素距离设定 因为该设定比较简单 防止以后忘记 所以贴 供自己查阅 1. 布局:main.xml <?xml version="1.0" encoding= ...

  7. sublime3的licence(update 2016-04-14)

    —– BEGIN LICENSE —–Michael BarnesSingle User LicenseEA7E-8213858A353C41 872A0D5C DF9B2950 AFF6F667C4 ...

  8. Linux防火墙设置(转载)

    From:http://www.lupaworld.com/article-219400-1.html (1) 重启后永久性生效: 开启:chkconfig iptables on 关闭:chkcon ...

  9. Python深入04 闭包

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 闭包(closure)是函数式编程的重要的语法结构.函数式编程是一种编程范式 (而 ...

  10. 庭审精彩语录整理 z

    公诉人:用百度搜索淫秽关键字+快播,搜索结果得出超过4200万结果,可见快播在传播淫秽视频方面的巨大影响.王欣:这个没有任何意义,您可以用百度搜索淫秽关键字+QQ看有多少结果. 新浪科技讯 1月8日下 ...