dice.py
Apr 05 2019- POSTED BY projecth
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 MoreThere is no foolproof way of protecting your code from outside use in Python.But if you follow the conventions in […]
Read MoreConstructions What if we need something more complex than just a custom init or new method? Constructors, as most classmethods […]
Read MoreSubclassing List Build a list that can be prefilled with a certain number of numbers. filledlist.py Testing _This underscore ignores […]
Read MoreSubclassing 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__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 MoreWhat if our instances should be able to be used with mathematical operations? We can define that! __init__In this program, […]
Read MoreBeing able to identify object classes and types is a really useful ability. isinstance(<object>, <class>) Tells you whether or not […]
Read MoreWe now have changed and updated the design of our app. Character Agile Sneaky |_______________|______| | Thief Tightly/Loosely Coupled Design […]
Read More__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 MoreObjects are nouns that can be described with adjectives (attributes) and can do things with verbs (methods). Classes are the […]
Read Moremonster.py import random from combat import Combat COLORS = [‘yellow’, ‘red’, ‘blue’, ‘green’] class Monster(Combat): min_hit_points = 1 max_hit_points = […]
Read MoreYou can override an inherited method in a class by simply re-defining it in the subclass. #[combat.py]: import random class […]
Read More#[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 MoreIt is a special method that is called whenever something is converted into a string. >>> from monster import Dragon >>> […]
Read MoreAdd New Attributes Set foreign values of corresponding keys as new attributes in the sent self instance, with the setattr( […]
Read More