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‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,
                  ‘8‘,‘9‘,‘10‘,‘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

时间: 2024-11-10 06:42:53

Class attributes的相关文章

论文笔记之:Deep Attributes Driven Multi-Camera Person Re-identification

Deep Attributes Driven Multi-Camera Person Re-identification 2017-06-28  21:38:55    [Motivation] 本文的网络设计主要分为三个部分: Stage 1: Fully-supervised dCNN training Stage 2: Fine-tuning using attributes triplet loss Stage 3:Final fine-tuning on the combined da

自定义html的attributes

本代码来自Coursera的Angular课程,欢迎大家去参加课程.我节选出精华的片段,写到自己博客上. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>HTML5 Custom Attributes</title> </head> <body> <h1>HTML5 Custom Attributes<

[翻译]NUnit---TearDown and SetUpFixture and Test Attributes(二十)

TearDownAttribute (NUnit 2.0 / 2.5) 本特性在TestFixture内部使用,每个测试方法执行后调用的方法集.也可以在SetUpFixture中使用,在同一命名空间或者程序集种相同的功能. NUnit2.5之前,类只能有一个TearDown方法且必须是示例方法. 从NUnit2.5开始,TearDown方法可以使静态或者示例方法,也可以在一个夹具中定义多个TearDown方法.通常多个TearDown方法只会在不同层级的继承中定义. 如果所有SetUp方法都正确

attributes[&quot;wv&quot;].nodeValue

w 获取自定义属性的值 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!DOCTYPE html> <html lang="en"> <head> <meta

THREE.js代码备份——webgl - custom attributes [lines](自定义字体显示、控制字图的各个属性)

<!DOCTYPE html> <html lang="en"> <head> <title>three.js webgl - custom attributes [lines]</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scal

iOS User Defined Runtime Attributes to define the key path

Next, add a button to the top right of each view controller. For both buttons, double-click and press backspace to set the button title to an empty string. Also set each button's background color to black. Now it's time to position the buttons with A

Deep Learning 论文笔记 (3): Deep Learning Face Attributes in the Wild

这是Xiaogang Wang和Xiaoou Tang组的一篇technical report,作者是优秀的学弟Ziwei Liu. 通常人脸识别里面先要对人脸图像进行检测和对齐,然后在相应的地方提取特征,但是在自然场景中,由于背景混乱,人脸检测和对齐会受到影响,进而影响特征提取和最后的识别效果. 这篇论文的主要思想是通过学习两个deep network来构建face attributes recognition的系统,其中第一个用来localization,第二个用来提取feature. 主要

[翻译]NUnit---TestCase Attributes(二十一)

TestCaseAttribute (NUnit 2.5) TestCase特性有两个效果,包括标记一个方法使用参数并且在调用的时候提供内置数据.示例如下,本示例会使用不同数据集执行3次: [TestCase(12,3,4)] [TestCase(12,2,6)] [TestCase(12,4,3)] public void DivideTest(int n, int d, int q) { Assert.AreEqual( q, n / d ); } Note:.net特性的限制了参数类型,N

[翻译]NUnit---SetUp and SetUpFixture and Suite Attributes(十九)

SetUpAttribute (NUnit 2.0 / 2.5) 本特性用于TestFixture提供一个公共的功能集合,在呼叫每个测试方法之前执行.同时也用在SetUpFixture中,SetUpFixture在相同命名空间或者程序集也实现相同的作用. 在NUnit2.5之前,类必须只能有一个SetUp方法且必须是一个实例方法. 从NUnit2.5开始,SetUp方法可以使一个静态或者实例方法,而且在一个Fixture可以多次使用.通常多个Setup方法定义在不同层级的继承. 如果一个Setu

Numeric Type Attributes

[Numeric Type Attributes]  INT(4) specifies an INT with a display width of four digits. This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them