Card objects
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的更多相关文章
- Think Python - Chapter 18 - Inheritance
In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...
- Class diagrams
So far we have seen stack diagrams, which show the state of a program, and object diagrams, which sh ...
- Decks
Now that we have Card objects, the next step is to define a class to represent decks. Since a deck i ...
- Class attributes
In order to print Card objects in a way that people can easily read, we need a mapping from the inte ...
- Django关于filter和get()方法
首先引入一个问题: 问: card = Card.objects.filter(pk=offline_card_id).get() card = Card.objects.get(pk=offline ...
- python测试开发django-37.外键(ForeignKey)查询
前言 前面在admin后台页面通过设置外键,可以选择下拉框的选项,本篇主要讲解关于外键(ForeignKey)的查询 models设计 在上一篇的基础上新增一个BankName表,Card表通过外键关 ...
- python测试开发django-36.一对一(OneToOneField)关系查询
前言 前面一篇在xadmin后台一个页面显示2个关联表(OneToOneField)的字段,使用inlines内联显示.本篇继续学习一对一(OneToOneField)关系的查询. 上一篇list_d ...
- [Django] ModelViewSet from rest_framework and Router
To build rest api easily, we can use ModelViewSet from rest_framework. It provides GET, POST, DELETE ...
- [Django] Building the rest API
Install the rest api framework: pip install djangorestfamework In settings.py: INSTALLED_APPS = [ 'd ...
随机推荐
- nand flash,nor flash,spi flash,片上RAM,片外RAM
Flash有掉电数据保存的特点,RAM掉电则数据丢失,但是RAM的速度更高,擦写次数理论上没有限制,而Flash则不行. Nand Flash相比其余的几种flash优势在于可擦写次数多,擦写速度快, ...
- JS截取后缀名,文件全名,非后缀名的方法---收藏(冷饭_)
<script language="javascript" type="text/javascript"> //取整个文件的路径并且把文件名赋给文件 ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- mysql函数计算地表两点间距离
DELIMITER $$ CREATE FUNCTION `test`.`getDistance`(LatBegin FLOAT(10,4), LngBegin FLOAT(10,4), LatEnd ...
- Java中的String与常量池
string是java中的字符串.String类是不可变的,对String类的任何改变,都是返回一个新的String类对象.下面介绍java中的String与常量池. 1. 首先String不属于8种 ...
- iOS 审核加急通道使用--转载来源--有梦想的蜗牛
提交完成后进入加急审核页面. 链接:https://developer.apple.com/appstore/contact/appreviewteam/index.html 在i would lik ...
- 修改Jenkins启动衍生进程的生命周期
Jenkins+jmeter 多线程测试java接口时爆错,导致无法生成html报告. 先介绍下场景: 在Jenkins中新建了一个Job,假设你在一些列Build Step之前/之后,启动了一个进程 ...
- spring和springmvc之间的整合
一.springmvc就是运行在spring的环境下,这两者是否需要进行整合,即:是不是要把service .dao . 事务 .和其它框架的整合放在springmvc的配置文件中.这样子在技术层面上 ...
- JavaScript解耦记
有两个页面A.B.页面A调用页面B.B页面还通过异步加载方式包含一个javascipt(例如叫:ClassHeaderEvaluation.js)文件.问题场景:A页面通过异步请求方式请求B,并在B的 ...
- Django 之 下载文件
法I: views.py #encoding:utf-8 import os from django.core.servers.basehttp import FileWrapper from dja ...