61V5UGMvslFFzr8nOB8W5O

Python Basics Lesson 4: Classes and Modules

article featured image

A CLASS is a logical grouping of data and functions. It gives the freedom to create data structures that contain arbitrary content to make things easily accessible. In Python CLASSES are defined by the class keyword:

class myClass(): def method1(self):

def method2(self,someString):

As we said we can use this to create a data structure using whatever content we want as well as creating any methods for that structure. For example let’s create a deck of cards. In this deck we will have 4 suites(Clubs, Diamonds, Hearts, and Spades), each with 13 cards(Ace through King).

class Deck: def init(self): self.contents = [Card(rank, suit) for rank in ranks() for suit in suites()] shuffle(self.contents)

def pop(self): return self.contents.pop().description()

def remaining(self): return len(self.contents)

As you may not yet notice, this Deck class relies on a Card class as well as the collections ranks() and suites(). This is seen on this line here: self.contents = [Card(rank, suit) for rank in ranks() for suit in suites()]

Let’s make these now:

def ranks(): return ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]

def suites(): return ["Clubs", "Diamonds", "Hearts", "Spades"]

class Card: def init(self, rank, suite): self.rank = rank self.suite = suite

In this example, there are a lot of things going on. The itit() methods are used to create an instance of the class. This is called a Constructor. In the case of the Card class, this init() method just sets the rank and the suite. In the Deck class the init() method creates a new shuffled deck of cards. The Deck class also has a method to get a card from the top of the deck and another to print the amount of cards left in the deck. Using this example we can then make many different card games such as blackjack, poker, etc by just adding the appropriate custom methods that are needed. For example, in blackjack we need a method that gives some custom numerical values for certain ranks:

class Card: def init(self, rank, suite): self.rank = rank self.suite = suite

def value(self, curvalue): if self.rank == "A": value = 11 if curvalue <= 10 else 1 elif self.rank == "J" or self.rank == "Q" or self.rank == "K": value = 10 else: value = int(self.rank) return value

Python CLASSES also support inheritance. Inheritance is a feature used in object-oriented programming, which refers to defining a new class with little to no modification to an existing class. The new class is called a derived class and the class from one which it inherits is called the base class. Python supports inheritance as well as supports multiple inheritances. A class can inherit attributes and behavior methods from another class called a subclass or heir class. This would allow us to make our Deck and Card classes above have all of the basic necessities that they need and then we can make a derived class from them to add more customized methods. For example our blackjack example above could be as so:

base class

class Card: def init(self, rank, suite): self.rank = rank self.suite = suite

derived class

class blackjackCard(Card): def value(self, curvalue): if self.rank == "A": value = 11 if curvalue <= 10 else 1 elif self.rank == "J" or self.rank == "Q" or self.rank == "K": value = 10 else: value = int(self.rank) return value

As seen in the above example, we now can make many of these derived classes, allowing for special features depending on whatever card game we are making, while keeping the basic structure of a deck of cards all the same.

This brings about the idea of Modules in Python. Modules are simply a way to define classes and methods in different files to make navigating and maintaining your codebase easier. For instance, with our deck of cards we can define our card and deck classes in a new module and import that module into our main application file. We can also import these classes in other files when and where we need to use them. For instance, we can create the Deck and Card class in a file called blackjack.py in a folder, on the same level as our main app.py file, called Games. You can download my version of blackjack.py here. For example:

app.py Games/ Blackjack.py

Then inside the Games folder create an empty file with the name init.py. Then in app.py you can import that module and use its methods as seen here:

from Games import blackjack

def deal(): global hands initshoe() hands = blackjack.deal(players, shoe) print(hands)

def initshoe(): numdecks = 8 global shoe if shoe is None or len(shoe) < .25 * numdecks * 52: shoe = blackjack.newshoe(numdecks)

Tags:

Comments

Leave a comment