Python: variables inside class methods

拈花ヽ惹草 提交于 2019-12-02 06:19:14

问题


I'm learning python and am trying to write a wound system based on hot zones of a character. Here's what I've written. Don't judge me too much.

class Character:
    def __init__ (self, agility, strength, coordination):
            self.max_agility = 100
            self.max_strength = 100
            self.max_coordination = 100
            self.agility = agility
            self.strength = strength
            self.coordination = coordination

    def hit (self, hit_region, wound):
            self.hit_region = hit_region
            self.wound = wound

            #Hit Zones
            l_arm=[]
            r_arm=[]
            l_leg=[]
            r_leg=[]
            hit_region_list = [l_arm , r_arm, l_leg, r_leg]


            #Wound Pretty Names
            healthy = "Healthy"
            skin_cut = "Skin Cut"
            muscle_cut = "Muscle Cut"
            bone_cut = "Exposed Bone"

            hit_region.append(wound)              

john = Character(34, 33, 33)

john.hit(l_arm, skin_cut)

I'd expect for the skin_cut input to be recognized as "Skin Cut", then added to l_arm, which I defined as a list. However, I always get a name error (l_arm is not defined). If i rewrite the method with the 'wound' as the first argument,the Name Error now comes with 'wound' as not defined. That kind of tells me it's something in the structure of the class I've missed, but I can't tell what.


回答1:


I change my previous answer to this.

class Character:
def __init__ (self, agility, strength, coordination):
        self.max_agility = 100
        self.max_strength = 100
        self.max_coordination = 100
        self.agility = agility
        self.strength = strength
        self.coordination = coordination
        self.l_arm=[]
        self.r_arm=[]
        self.l_leg=[]
        self.r_leg=[]
        self.hit_region_list = [self.l_arm , self.r_arm, self.l_leg, self.r_leg]
        self.healthy = "Healthy"
        self.skin_cut = "Skin Cut"
        self.muscle_cut = "Muscle Cut"
        self.bone_cut = "Exposed Bone"

def hit (self, hit_region, wound):
        self.hit_region = hit_region
        self.wound = wound
        hit_region.append(wound)
        #Hit Zones



        #Wound Pretty Names




john = Character(34, 33, 33)

john.hit(john.l_arm,john.skin_cut)

print john.hit_region
print john.l_arm

After running the above code I got this output

output:
['Skin Cut']
['Skin Cut']

As per the post, I think this is what you wanted. According to your previous code, your declarations were accessible inside a function only. Now You can manipulate the data and these variables for particular instances by declaring them inside the constructor.




回答2:


You define l_arm in function and its local to that function only. It has scope of function only. That can be accessed inside function only.

You try to access l_arm outside function, and that gives error, l_arm is not defined.

If you want to access all these variable outside function, you can define it above class

#Hit Zones
l_arm=[]
r_arm=[]
l_leg=[]
r_leg=[]
hit_region_list = [l_arm , r_arm, l_leg, r_leg]


#Wound Pretty Names
healthy = "Healthy"
skin_cut = "Skin Cut"
muscle_cut = "Muscle Cut"
bone_cut = "Exposed Bone"

class Character:
    ...
    ...
    ...

john = Character(34, 33, 33)

john.hit(l_arm, skin_cut)

This will work.




回答3:


Every local variable assigned within a function is thrown away as soon as the function ends. You need to prepend self. to those names so that they are saved as instance variables, such as self.l_arm, self.r_arm, and so on. The same goes for the wound pretty names, if you plan on using those objects later on.



来源:https://stackoverflow.com/questions/36109635/python-variables-inside-class-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!