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. sublime text 3 安装注释

    sublime text 3 1.安装Sublime Text 3  下载安装:http://www.sublimetext.com/3 Package Control安装:https://subli ...

  2. ERP_基于Oracle ADF的定制化企业级IT系统解决方案

    2014-12-31 Created By BaoXinjian

  3. ERP_Oracle Erp 11i 和 R12的区别概述(概念)

    2014-06-26 Created By BaoXinjian

  4. NeHe OpenGL教程 第十八课:二次几何体

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

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

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

  6. springmvc 中RequestMapping注解的使用

    1.RequestMapping注解既可以修饰方法,又可以修饰类型,类型指定的url相对于web跟路径,而方法修饰的url相对于类url: 2.RequestMapping的几个属性: value:用 ...

  7. [Java] 字符流 Writer,输出字符数据PrintWriter

    package test.stream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Fi ...

  8. [Java] 过滤文件夹

    package test.file; import java.io.File; import java.io.FileFilter; /** * 过滤文件夹 * @author Frost.Yen * ...

  9. [ActionScript 3.0] flash如何访问父级或者舞台上的变量、函数等的方法

    方法一: 进行类型转换,先将root.parent强制转换为MovieClip类型,再进行使用,如下:MovieClip(root).i.MovieClip(this.parent).i.MovieC ...

  10. Microsoft Visual C++ 2015 Redistributable(x64) - 14.0.2306 设置失败

    想要在Windows 2008 R2 中 安装PHP, 需要安装 Microsoft Visual C++ 2015 Redistributable(x64) ,结果提供设置失败. 先中找到以下文字, ...