dice.py

For Python Testing lessons. import random import re die_pattern = re.compile(r’^(?P<num>\d+)d(?P<sides>\d+) , re.I) class Die: value = None def __init__(self, […]

Read More

Exercises

Exercise: Subclassing int Alright, time to subclass int.Make a class named Double that extends int. Now override new. Create a new int […]

Read More

Method Protection

There is no foolproof way of protecting your code from outside use in Python.But if you follow the conventions in […]

Read More

Classmethods

Constructions What if we need something more complex than just a custom init or new method? Constructors, as most classmethods […]

Read More

Subclassing Built-ins: List & Dict

Subclassing List Build a list that can be prefilled with a certain number of numbers. filledlist.py Testing _This underscore ignores […]

Read More

Subclassing Built-ins: Strings

Subclassing String We used init when we wanted to customize how a new instance of a class is created. new works very similarly. The […]

Read More

Iterable Object

__len__Gives your object a collection length. __contains__For checking membership (e.g. using in). __iter__Makes your object iterable. yield lets you send data back […]

Read More

Magic Methods

What if our instances should be able to be used with mathematical operations? We can define that! __init__In this program, […]

Read More

Class Family Tree

Being able to identify object classes and types is a really useful ability. isinstance(<object>, <class>) Tells you whether or not […]

Read More

Multiple Inheritance

We now have changed and updated the design of our app. Character Agile Sneaky |_______________|______| | Thief Tightly/Loosely Coupled Design […]

Read More

Inheritance and __init__

__init__ of class def __init__(self, name, sneaky=True, **kwargs): self.name = name self.sneaky = sneaky for key, value in kwargs.item(): setatter(self, […]

Read More

OOP Explained

Objects are nouns that can be described with adjectives (attributes) and can do things with verbs (methods). Classes are the […]

Read More

APPENDIX

class Monster: hit_points = 1 sound = ‘roar’ … def battlecry(self): return self.sound.upper() >>> from monster import Monster >>> jubjub […]

Read More
Comments Off on APPENDIX

Hack-n-Slash [Game]

monster.py import random from combat import Combat COLORS = [‘yellow’, ‘red’, ‘blue’, ‘green’] class Monster(Combat): min_hit_points = 1 max_hit_points = […]

Read More
Comments Off on Hack-n-Slash [Game]

Overriding Inheritance

You can override an inherited method in a class by simply re-defining it in the subclass. #[combat.py]: import random class […]

Read More
Comments Off on Overriding Inheritance

Instance Methods

#[character.py] class Character: experience = 0 hit_points = 10 def get_weapon(self): weapon_choice = input(“Weapon ([S]word, [A]xe, [B]ow: “).lower() if weapon_choice […]

Read More
Comments Off on Instance Methods

__str__( )

It is a special method that is called whenever something is converted into a string. >>> from monster import Dragon >>> […]

Read More
Comments Off on __str__( )

Inheritance

Add New Attributes Set foreign values of corresponding keys as new attributes in the sent self instance, with the setattr( […]

Read More
Comments Off on Inheritance

Objects

Class A class is a collection of attributes and methods that can be instantiated. class Monster: hit_points = 1 color […]

Read More
Comments Off on Objects