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. Form_通过Trace分析Concurrent和Form性能和异常详解(案例)

    2014-06-21 Created By BaoXinjian

  2. socket 发送发送HTTP请求

    socket方式: $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //socket_set_option($socket, SOL_S ...

  3. CLR via C#笔记

    第一部分 CLR基础 CLR和JIT “运行时”如何解析类型引用 第二部分 设计类型 使用C#的is和as操作符来转型 运行时的相互联系 基元类型.引用类型和值类型 值类型的装箱和拆箱 哈希码 dyn ...

  4. 图片--Android加载图片导致内存溢出(Out of Memory异常)

    Android在加载大背景图或者大量图片时,经常导致内存溢出(Out of Memory  Error),本文根据我处理这些问题的经历及其它开发者的经验,整理解决方案如下(部分代码及文字出处无法考证) ...

  5. Win7玩游戏偶尔自动跳转到桌面的解决办法[转]

    新装的win7旗舰版SP1,怎么玩wow (魔兽世界.极品飞车.全屏游戏.按键精灵.挂机)总是过一会就自己返回桌面了.刚开始以为是显卡的毛病,更新了驱动还是一样(在这之前,排除病毒,其他驱动问题).因 ...

  6. JAVA 文本框、密码框、标签

    //文本框,密码框,标签 import java.awt.*; import javax.swing.*; public class Jiemian5 extends JFrame{ JPanel m ...

  7. android之location02

    package com.example.mars_3300_location02; import java.net.ContentHandler; import java.util.List; imp ...

  8. TableViewCell的分割线显示不全解决方法

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath ...

  9. spring整合struts

    整合目标:使用spring的bean管理struts action service. 整合步骤: 一.加入spring 1.加入spring jar包 2.配置web.xml文件 <contex ...

  10. #获取本机IP地址时排除IPv6类型,只返回IPv4地址的方法

    public static string GetLocalIP(){try{string HostName = Dns.GetHostName(); //得到主机名IPHostEntry IpEntr ...