Python: variables inside class methods

后端 未结 3 563
情深已故
情深已故 2021-01-23 04:13

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 Charact         


        
3条回答
  •  猫巷女王i
    2021-01-23 05:05

    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.

提交回复
热议问题