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. hostapd移植与使用

    介绍 版本:hostapd-2.5.tar.gz 下载地址:http://w1.fi/releases/hostapd-2.5.tar.gz 依赖:libnl openssl 移植 libnl移植 w ...

  2. SVN Server配置详解 及备份

    SVN简介和工作原理 subversion(简称svn)是近几年崛起的版本管理软件,是cvs的接班人,目前绝大多数开源软件都使用svn作为代码版本管理软件.Subversion支持linux和wind ...

  3. DBA_Oracle数据库运维监控(案例)

    2014-07-27 Created By BaoXinjian

  4. Web Penetration Testing 渗透 通信信息安全

    参考 http://download.csdn.net/detail/jason571/8146587 一.操作系统原理,JavaScript,http/https的运行原理,Ajax,SQL等技术基 ...

  5. TextView的属性列表

    网上收集和自己整理的TextView控件中可选择的属性列表,经常可以用到的: android:autoLink       设置是否当 文本为URL链接/email/电话号码/map时,文本显示为可点 ...

  6. sax,Dom,等解析方式地址 ?

    Android中使用SAX对XMl文件进行解析 http://blog.csdn.net/developer_jiangqq/article/details/7197045 使用SAX技术对XML文档 ...

  7. ERWin & ERStudio图里的实线和虚线的含义[转]

    注: ERWin 与 ERStudio 中这一点的描述方法是一样的. ERWin里面线代表实体间的三种关系:决定关系(Identifying Relationship),非决定关系(None-Iden ...

  8. RPM软件包制作

    参考网站:How to create an RPM package/zh-cn[教程]https://fedoraproject.org/wiki/How_to_create_an_RPM_packa ...

  9. C++学习22 多态的概念及前提条件

    在<C++基类和派生类的赋值>一节中讲到,基类的指针也可以指向派生类对象.请看下面的例子: #include <iostream> using namespace std; c ...

  10. 服务器返回数组,data[0]得到的总是不对?如何处理?

    我用asp.net MVC 写了服务器代码,返回数组,ajax怎么处理返回的数组内容? 您好,首先用eval将获得的ajax数据转化成json对象并赋值给一个变量. 比如: var obj=eval( ...