Learn to Think Like a Programmer: python programming for beginners by Paul Elias

Learn to Think Like a Programmer: python programming for beginners by Paul Elias

Author:Paul, Elias [Paul, Elias]
Language: eng
Format: azw3, epub
Published: 2020-06-16T16:00:00+00:00


Chapter 15

Object sets

15.1. Composition

So far, you have already seen several examples of composition. One of the first Examples was the use of a method call as part of an expression.

Another example is the nested structure of statements; you can write a if statement inside a while loop, inside another if statement, and so on.

sively.

Having seen this pattern, and knowing about lists and objects, you should not surprising that you can create lists of objects. You can also create objects that contain lists (in the form of attributes); you can create lists containing lists;

objects containing objects, and so on indefinitely.

In this chapter and the next, we will explore some examples of these combinations nations, and we will use Letter objects as an example.

15.2. Letter Objects

If you are not familiar with common playing cards, it may be a good time for me to get a deck, otherwise this chapter may not have much sense. There are fifty-two cards in an English deck, each of which belongs to a stick and has a value; there are four different sticks and thirteen values. The suits are Spades, Hearts, Diamonds, and Clubs (in the order descending according to the bridge). Values are As, 2, 3, 4, 5, 6, 7, 8, 9, 10, Sota, King and queen. Depending on the type of game being played, the Ace value may be greater than the King or less than 2.

Page 188

162

Object sets

If we want to define a new object to represent a card, it is obvious what butos should have: value and suit. What is not so obvious is the type that should give attributes. One possibility is to use character strings that contain words like "Spades" for the sticks and "Queen" for the values. A problem of this implementation is that it will not be easy to compare cards to see which one has higher value or suit.

An alternative is to use integers to encode the values and suits. With the term "encode" we don't mean what some people can think about encrypting or translating into a secret code. What a programmer understand by "encode" is "define a correspondence between a sequence of numbers and the elements to be represented ”. For example: Spades

↦ → 3

Hearts

↦ → 2

Diamonds ↦ → 1

Clovers

↦ → 0

This correspondence has an obvious characteristic: the sticks correspond to integers in order, so we can compare the suits by comparing the numbers. The association of values is quite obvious; each of the numerical values are associated with the corresponding integer, and for the figures: Jack

↦ → 11

Queen ↦ → 12

King

↦ → 13

We are using mathematical notation for these associations for a Reason: They are not part of the Python program. They are part of the design of the program ma, but they never appear explicitly in the source code. The definition of

class for letter type will look like: class Letter:

def __init __ (self, stick = 0, value = 0):

self.stick = stick

self.value = value

As usual, we will provide an initialization method that takes an optional parameter for each attribute.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.