in my main.py this works..... cur_weapon is set to the axe object and I can use it..
import weapon
cur_weapon = weapon.axe
print(cur_weapon)
i have a class and created the axe object in weapon.py
this will not.. ...
import weapon
cur_choice = "axe"
cur_weapon = weapon.cur_choice
print(cur_weapon)
AttributeError: module 'weapon' has no attribute 'cur_choice'
do i need to 'dereference' it somehow? (unsure term) additionally, any pointers on how to describe this would be great.. (new to python)
weapon.py
from random import randint
class Weapon:
"""initalize weapon, defaults to the humble dagger"""
def __init__(self,
name = "humble dagger",
two_hand = False,
die_dmg = 6,
rolls = 1,
magic_mod = 0,
dmg_type = "slash",
speed = 0,
dur = 100,
wid = 99):
self.name = name
self.two_hand = two_hand
self.die_dmg = die_dmg
self.rolls = rolls
self.magic_mod = magic_mod
self.dmg_type = dmg_type
self.speed = speed
self.dur = dur
self.wid = wid
def attack_dmg(self):
hit = (randint(1, self.die_dmg) * self.rolls) self.magic_mod
print(f"Your {self.name} hits for {hit} damage")
return hit
axe = Weapon("axe", False, 6, 2, 0, "slash", 100, 0, 101)
CodePudding user response:
You can use getattr
cur_weapon = getattr(weapon, cur_choice)
This helps to get the attribute when you have a string for the attribute name.