So I've recently come to an understanding with inheritance for classes and have come to appreciate their uses. So I began coding a concrete example up for myself.
I realized as I was making multiple classes for different weapons that it would be easier if I could have a class that generated these classes for me. I'm a little stuck on how to get this done.
I have search around and it gets confusing and theoretical quick. This example on this site (http://en.wikibooks.org/wiki/Python_Programming/Metaclasses) under "Class Factories" seems to be what I need, but I seem to be missing something.
Here's what I have:
class item(object):
def __init__(self, item_name, item_type):
self.item_type = item_type
self.item_name = item_name
def get_item_name(self):
return self.item_name
def get_item_type(self):
return self.item_type
def __str0__(self):
return "%s is a %s" % (self.item_name, self.item_type)
class weapon(item):
def __init__(self, name, weapon_type, dmg_mod, atk_typ):
item.__init__(self, name, "weapon")
self.name = name
self.weapon_type = weapon_type
self.dmg_mod = dmg_mod
self.atk_typ = atk_typ
def get_wpn_type(self):
return self.weapon_type
def get_dmg_mod(self):
return self.dmg_mod
def get_atk_typ(self):
return atk_typ
def __str1__(self):
return "%s is a %s with %s type damage at a %i modifier" % (self.name,
self.weapon_type,
self.atk_typ,
self.dmg_mod)
def weapon_fact(weptype, dmg_type):
class weptype(weapon):
def __init__(self, name, dmg_mod):
weapon.__init__(self, name, weptype, dmg_mod, dam_type)
self.name = name
When I do
longbow = weapon_fact("Long Bow", "Pierce")
huntersbow = longbow("Hunter's Bow", 5)
I get this traceback
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
huntersbow = longbow("Hunter's Bow", 5)
TypeError: 'NoneType' object is not callable
And when I try to call longbow it has None assigned to it.