from thieves import Thief ryan = Thief(name="Ryan", sneaky=False) print(ryan) print(ryan.sneaky) print(ryan.agile) print(ryan.hide(8))
from thieves import Thief kenneth = Thief(name="Kenneth", sneaky=False) print(kenneth.sneaky) print(kenneth.agile) print(kenneth.hide(8)) # isinstance('a', str) -> True # isinstance(5.2, (str, bool, dict)) -> False # issubclass(bool, int) -> True # issubclass(str, int) -> False # issubclass(Thief, Character) -> True type(kenneth) # returns <class 'thieves.Thief'> kenneth.__class__ # returns <class 'thieves.Thief'> kenneth.__class__.__name__ # returns 'Thief'
from characters import Character from thieves import Thief """ isinstance(<object>, <class>) This function will tell you whether or not <object> is an instance of <class>. If it could be an instance of several classes, you can use a tuple of classes like so: isinstance(<object>, (<class1>, <class2>, <class3>)). issubclass(<class>, <class>) This function will tell you whether or not one class is a descendent of another class. Just like isinstance(), you can use a tuple of classes to compare against. type(<instance>) will give you the class for an instance, as will instance.__class__. Neither of these is particularly useful. """ tom = Thief(name="Tom") print("Is Tom a Thief? {}".format(isinstance(tom, Thief))) # True print("Is Tom a Character? {}".format(isinstance(tom, Character))) # True print("Is a thief a character? {}".format(issubclass(Thief, Character))) # True print("Is a character a thief? {}".format(issubclass(Character, Thief))) # False print("What is tom? {}".format(type(tom))) print("What is tom in a nicer format? {}".format(tom.__class__.__name__)) lineage = [] for x in tom.__class__.__mro__: lineage.append(x.__name__)
from thieves import Thief pinecone = Thief(name="Mr. Pinecone") print(pinecone.sneaky) print(pinecone.agile) print(pinecone.hide(6))
from thieves import Thief matt = Thief(name="Matt", sneaky=False) print(matt) print(matt.sneaky) print(matt.agile) print(matt.hide(8))
from thieves import Thief josh = Thief("Josh", sneaky=False) print(josh.sneaky) print(josh.agile) print(josh.hide(8))
from thieves import Thief kenneth = Thief(name='Kenneth', sneaky=False) print(kenneth) print(kenneth.sneaky) print(kenneth.agile) print(kenneth.hide(8))
from thieves import Thief daesy = Thief(name="Daesy", sneaky=False) print(daesy) print(daesy.sneaky) print(daesy.agile) print(daesy.hide(8))
from thieves import Thief jonnie = Thief(name="Jonnie", sneaky=False) print(jonnie.sneaky) print(jonnie.agile) print(jonnie.hide(8))
from thieves import Thief jimmy = Thief(name="Jimmy", sneaky=False) print(jimmy.sneaky) print(jimmy.agile) print(jimmy.hide(8))
from thieves import Thief taylor = Thief(name="taylor", sneaky=False) print(taylor) print(taylor.sneaky) print(taylor.agile) print(taylor.hide(12))
from thieves import Thief ron = Thief(name = "ron", sneaky = False) print(ron.sneaky) print(ron.agile) print(ron.hide(8))
from thieves import Thief from characters import Character kenneth = Character(name="Kenneth", sneaky=True, agile=True) print(kenneth) print(kenneth.sneaky) print(kenneth.agile) #print(kenneth.hide(8)) #print(kenneth.pickpocket()) print('x') #print(Thief.pickpocket(kenneth)) x = Thief(name='name') print(x.pickpocket()) print(x) print('asd') #print(kenneth.evade()) #print(kenneth.hide(11))
from thieves import Thief rock = Thief(name="rock", scar=None, weapon="wit") jack = Thief("jack") print(rock.agile) print(rock.sneaky) print(jack.hide(11))
from thieves import Thief Tesh = Thief(name="Tesh") print(Tesh.name) print(Tesh) print(Tesh.sneaky) print(Tesh.agile)
from thieves import Thief kevin = Thief(name="Kevin", sneaky=False) print(kevin.sneaky) print(kevin.agile) print(kevin.hide(8))
class Character: def __init__(self, name, **kwargs): if not name: raise ValueError("name required") self.name = name for key, value in kwargs.items(): setattr(self, key, value) # attributes.py import random class Sneaky: sneaky = True def __init__(self, sneaky=True, *args, **kwargs): super().__init__(*args, **kwargs) self.sneaky = sneaky def hide(self, light_level): return self.sneaky and light_level < 10 class Agile: agile = True def __init__(self, agile=True, *args, **kwargs): super().__init__(*args, **kwargs) self.agile = agile def evade(self): return self.agile and random.randint(0, 1) # thieves.py import random from attributes import Agile, Sneaky from characters import Character class Thief(Character, Agile, Sneaky): def pickpocket(self): return self.sneaky and bool(random.randint(0, 1)) # play.py from thieves import Thief kenneth = Thief(name="Kenneth", sneaky = False) print(kenneth.seaky) #False print(kenneth.agile) #True print(kenneth.hide(8)) #False #exercise class Inventory: def __init__(self): self.slots = [] def add_item(self, item): self.slots.append(item) class SortedInventory(Inventory): def add_item(self, item): super().add_item(item) #override the parent calss Inventory self.slots.sort()
from thieves import Thief anthony = Thief(name="Anthony", sneaky=False) print(anthony) print(anthony.sneaky) print(anthony.agile) print(anthony.hide(8))
from thieves import Thief tom = Thief(name="Tom", sneaky=False) # name is now a keyword so that it is not simply positional in the init print("{name} sneaky? {sneaky}".format(name=tom.name, sneaky=tom.sneaky)) print("{name} agile? {agile}".format(name=tom.name, agile=tom.agile)) print("{name} hide? {hide}".format(name=tom.name, hide=tom.hide(8))) # john=Thief(sneaky=True) # this will cause this exception # ValueError: 'name' is required # from Character init # inspect.getmro() or class.__mro__ can be used to a class's method # resolution order (MRO) print(Thief.__mro__) # (<class 'thieves.Thief'>, <class 'attributes.Agile'>, # <class 'attributes.Sneaky'>, <class 'characters.Character'>, <class 'object'>) print(tom) # running print on tom before the Character class was modified # returns <thieves.Thief object at 0x00A8E1D0> # after updating the Character class, when print is called on tom, it # returns Thief: Tom print(repr(tom)) # running this returns <thieves.Thief object at 0x00A8E1D0> # which is what print(tom) did previously # this can also be overridden with __repr__
from thieves import Thief sean = Thief(name="Sean", sneaky=False) print(sean.sneaky) print(sean.agile) print(sean.hide(8))
from thieves import Thief sepehr = Thief(name='sepehr', sneaky=False) print(sepehr.sneaky) print(sepehr.agile) print(sepehr.hide(8))
from thieves import Thief casey = Thief(name="Casey", sneaky=False) print(casey) print(casey.sneaky) print(casey.agile) print(casey.hide(8))
isinstance('a', str) # will tell you if something is an instance of an object True isinstance(5.2, (str, book, dict)) False isinstance(True, int) True # why? I think because 0 is false and 1 is true? # I'm right. google issubclass(bool, int) True # will take a list or tuple to compare against from thieves import Thief from characters import Character issubclass(Thief, Character) True kenneth = Thief(name="Kenneth") type(kenneth) <class 'thieves.Thief'> # but this won't tell us if he's a character...
from thieves import Thief viktor = Thief(name = "Viktor", sneaky=False) print(viktor) print(viktor.sneaky) print(viktor.agile) print(viktor.hide(8))
from thieves import Thief instance = Thief(name='Oskar', sneaky=False) print(instance) print(instance.sneaky) print(instance.agile) print(instance.hide(8))
from thieves import Thief from inventory import Inventory from items import Item from items import Weapon charles = Thief(name="Charles", sneaky=False) print(charles.sneaky) print(charles.agile) print(charles.hide(8)) print("\n\n") print("Printing Class and Instance information using __str__.") print(charles) # instantiate an Item object coin = Item('coin', 'a gold coin') # instantiate an Inventory object inventory = Inventory() inventory.add(coin) print("You have {} item(s) in your inventory.".format(len(inventory))) if coin in inventory: print("You have a gold coin in your inventory.") sword = Weapon('sword', '2h sharp sword', 50) inventory = Inventory() inventory.add(sword) for item in inventory: print("Item Name: {} - Item Description: {}".format( item.name, item.description))
from thieves import Thief gimp = Thief(name='Omair', sneaky=False) print(gimp) print(gimp.sneaky) print(gimp.agile) print(gimp.hide(5))
from thieves import Thief kortney = Thief("kortney", sneaky=False) print(kortney.sneaky) print(kortney.agile) print(kortney.hide(8))
from thieves import Thief prabhakar = Thief(name='Prabhakar', sneaky=True) print(prabhakar.sneaky) print(prabhakar.agile) print(prabhakar.hide(8))
from thieves import Thief shiv = Thief(name="shiv", sneaky=False) print(shiv) print(shiv.name) print(shiv.sneaky) print(shiv.agile)