Python:类方法中的变量 [英] Python: variables inside class methods

查看:141
本文介绍了Python:类方法中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学Python,并且正在尝试写一个基于角色热区的伤口系统。这是我写的。

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)

我期望skin_cut输入被识别为Skin Cut,然后添加到我定义为列表的l_arm中。但是,我总是得到一个名称错误(l_arm未定义)。如果我用'伤口'作为第一个参数来重写方法,名称错误现在以未定义的'伤口'出现。这种告诉我这是我错过的课程结构中的一些东西,但我不知道是什么。

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.

推荐答案

我改变了我以前的答案。

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.

这篇关于Python:类方法中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆