There are fifty-two cards in a deck, each of which belongs to one of four suits and one of thirteen ranks. The suits are Spades, Hearts, Diamonds, and Clubs (in descending order in bridge). The ranks are Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, and King. Depending on the game that you are playing, an Ace may be higher than King or lower than 2.

If we want to define a new object to represent a playing card, it is obvious what the attributes should be: rank and suit. It is not as obvious what type the attributes should be. One possibility is to use strings containing words like ‘Spade’ for suits and ‘Queen’ for ranks. One problem with this implementation is that it would not be easy to compare cards to see which had a higher rank or suit.

An alternative is to use integers to encode the ranks and suits. In this context, ‘encode’ means that we are going to define a mapping between numbers and suits, or between numbers and ranks. This kind of encoding is not meant to be a secret.

For example, this table shows the suits and the corresponding integer codes:

Spades -> 3

Hearts -> 2

Diamonds -> 1

Clubs -> 0

This code makes it easy to compare cards; because higher suits map to higher numbers, we can compare suits by comparing their codes.

The class definition for Card looks like:

class Card:
""" represents a standard playing card.""" def __init__(self, suit=0, rank=2):
self.suit = suit
self.rank = rank

As usual, the init method takes an optional parameter for each attribute. The default card is the 2 of Clubs.

from Thinking in Python

Card objects的更多相关文章

  1. Think Python - Chapter 18 - Inheritance

    In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...

  2. Class diagrams

    So far we have seen stack diagrams, which show the state of a program, and object diagrams, which sh ...

  3. Decks

    Now that we have Card objects, the next step is to define a class to represent decks. Since a deck i ...

  4. Class attributes

    In order to print Card objects in a way that people can easily read, we need a mapping from the inte ...

  5. Django关于filter和get()方法

    首先引入一个问题: 问: card = Card.objects.filter(pk=offline_card_id).get() card = Card.objects.get(pk=offline ...

  6. python测试开发django-37.外键(ForeignKey)查询

    前言 前面在admin后台页面通过设置外键,可以选择下拉框的选项,本篇主要讲解关于外键(ForeignKey)的查询 models设计 在上一篇的基础上新增一个BankName表,Card表通过外键关 ...

  7. python测试开发django-36.一对一(OneToOneField)关系查询

    前言 前面一篇在xadmin后台一个页面显示2个关联表(OneToOneField)的字段,使用inlines内联显示.本篇继续学习一对一(OneToOneField)关系的查询. 上一篇list_d ...

  8. [Django] ModelViewSet from rest_framework and Router

    To build rest api easily, we can use ModelViewSet from rest_framework. It provides GET, POST, DELETE ...

  9. [Django] Building the rest API

    Install the rest api framework: pip install djangorestfamework In settings.py: INSTALLED_APPS = [ 'd ...

随机推荐

  1. web几个高性能框架的简单测试

    参考的这里 压测工具 wrk -t16 -c100 -d30s http://127.0.0.1:8080/rest/hello 测试代码 package main import ( "st ...

  2. c++的历史-异常

    1.异常出现的目的 在c++语言的设计和演化中,Bjarne Stroustrup说过异常的设计假定如下情况: 基本上是为了处理错误 与函数定义相比,异常处理是很少的 与函数调用相比,异常出现的频率较 ...

  3. SqlServer——阻止保存要求重新创建表的更改

    场景: 修改已有数据的列宽时,提示“阻止保存要求重新创建表的更改”. 解决: 工具-〉选项-〉左侧有个 设计器-〉表设计器和数据库设计器 -> 阻止保存要求重新创建表的更改(右侧) 把钩去掉即可 ...

  4. Git-Flow

    Overview Git-Flow is a high-level command set wrapping low-level Git commands to support the "s ...

  5. [CSS]元素的显示与隐藏

    摘自:http://www.zhangxinxu.com/wordpress/?p=1528 元素隐藏与显示是我们在页面制作与交互效果实现中非常常见的,如果您只是使用display:none与disp ...

  6. XHTML的使用规范

    一.XHTML的简介 XHTML指的是可扩展超文本标记语言 XHTML与HTML4.01几乎是相同的 XHTML是更严格更纯净的HTML版本 XHTML是以XML应用的方式定义的HTML 二.为什么使 ...

  7. 程序进入 EXPORT App_Fault_ISR的原因及措施:

    最近再UCOSIII+LPC1768上移植modbus,在定时器初始化部分竟然跑飞进入 EXPORT  App_Fault_ISR,查资料.逛论坛.问大牛都没有解决,最后发现竟然是犹豫一个低级失误引起 ...

  8. python 正则表达式 demo

    1.匹配大小写和数字,并且大小写数字均要有,且字符串长度为6~20位 ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{6,20}$ import repattern ...

  9. memcached完全剖析–1. memcached的基础

    系列文章导航: memcached完全剖析–1. memcached的基础 memcached全面剖析–2. 理解memcached的内存存储 memcached全面剖析–3. memcached的删 ...

  10. Nginx+Tomcat+Keepalived+Memcache 负载均衡动静分离技术

    一.概述 Nginx 作负载均衡器的优点许多,简单概括为: ①实现了可弹性化的架构,在压力增大的时候可以临时添加Tomcat服务器添加到这个架构里面去; ②upstream具有负载均衡能力,可以自动判 ...