简体   繁体   English

哪种创建类属性的方法更好,为什么?

[英]Which way of creating class attributes is better and Why?

Only a couple of months or so into programming (Python) and I'm trying to wrap my head around this so that I don't end up writing inefficient code. 编程(Python)只有几个月左右的时间,我正试图绕过这个问题,这样我就不会编写低效的代码了。

Let's say I'm creating classes for default enemies in a game. 假设我正在为游戏中的默认敌人创建类。 I use 'ENEMY01' and 'HERO01' just for clarity purpose (they both can be consider the 'same' class written differently). 我使用'ENEMY01'和'HERO01'只是为了清晰起见(他们都可以认为'同一'类的写法不同)。 Consider this pseudo code; 考虑这个伪代码; if there are any errors, it's just because I missed them in this post and I'm not having problems with the code itself. 如果有任何错误,那只是因为我在这篇文章中错过了它们,而且我没有遇到代码本身的问题。 If you have a more efficient way of writing what I wrote though, it will be appreciated. 如果你有更有效的方式来写我写的东西,我们将不胜感激。

Example 1- 例1-

class ENEMY01:
    attack = randint(0,20)
    defense = randint(1,20)
    health = 100

Example 2- 例2-

class HERO01:
    def __init__(self, attack=randint(1,20),defense=randint(1,20), health=100)
        self.attack = attack
        self.defense = defense
        self.health = health

It seems to me that Example 1 is better because it's shorter and easier to understand. 在我看来,示例1更好,因为它更短,更容易理解。 With Example 2, because of the default values, you can either stick with the defaults or change them at instantiation. 使用示例2,由于默认值,您可以坚持使用默认值,也可以在实例化时更改它们。 I understand that may be a minor benefit, but I noticed you can also change those default values right after creation with both examples anyway- 我理解这可能是一个小小的好处,但我注意到你也可以在创建后立即用这两个例子改变那些默认值 -

Jobo = ENEMY01()
Jobo.attack
10
Jobo.attack = 15
Jobo.attack
15

Bobo = HERO01()
Bobo.attack
10
Bobo.attack = 15
Bobo.attack
15

And if I wanted to add a new attribute to either (I think that's called instance attribute, since it doesn't change the class), there doesn't seem to be a limit on either- 如果我想添加一个新属性(我认为它被称为实例属性,因为它不会改变类),似乎没有任何限制 -

Bobo.scream = 'Ouch'
Bobo.scream
'Ouch'
Jobo.scream = 'Ouch'
Jobo.scream
'Ouch'

My noob guess is that because Example 2 uses methods (as in 'function'), I could theoretically create instances of Example 2 that are capable of reacting to new 'information' by adding more capability to the function in the class (ways for it to react to certain attacks or conditions, for example). 我的noob 猜测是因为示例2使用了方法(如'function'),理论上我可以通过在类中为函数添加更多功能来创建示例2的实例,这些实例能够对新的'信息'作出反应(对于它的方法)例如,对某些攻击或条件做出反应。 I'm thinking that if I go the Example 1 route, I would have to add that type of reactive code each time I need it where I need it, instead of letting it 'live' in the class method? 我想如果我去示例1路由,我每次需要时都需要添加那种类型的反应代码,而不是让它在类方法中“活”吗? Except couldn't I fix that problem by adding functions to Example 1? 除非无法通过向示例1添加函数来解决该问题?

So what are the pros and cons of Example 2 over Example 1 (or vice versa) as written or is there a better way to rewrite either to give one an impossible to match advantage over the other? 那么示例2的优缺点是什么,而不是示例1(或反之亦然),或者是否有更好的方法来重写,以使一个人无法匹配另一个人的优势?

Sidenote - I'm going through a tutorial right now and it seems that even though he started with Example 1, and has moved on to the style in Example 2, that I can still accomplish everything he's doing with example one (except for the changing of values at instantiation), and yet he's making it seem (without explanation) that Example 2 is so much more superior and different. 旁注 - 我现在正在阅读一个教程,看起来即使他从示例1开始,并且已经转到示例2中的样式,我仍然可以用示例1完成他所做的一切(除了更改)在实例化时的价值观,但他似乎(没有解释)示例2是如此优越和不同。 Hence my question. 因此我的问题。 Maybe the ability to changes values at instantiation is more important than I'm giving it credit for? 也许在实例化时改变价值的能力比我给它的信誉更重要?

Neither is "better," because they're different. 两者都不“更好”,因为它们不同。

class ENEMY01:
    def __init__(self):
        self.attack = randint(0,20)
        self.defense = randint(1,20)
        self.health = 100

For the above, each new instance will have different random values. 对于上述情况,每个新实例将具有不同的随机值。

class HERO01:
    def __init__(self, attack=randint(1,20),defense=randint(1,20), health=100)
        self.attack = attack
        self.defense = defense
        self.health = health

For the above, each new instance will get the same default values, changing on successive runs of the program. 对于上述情况,每个新实例将获得相同的默认值,在程序的连续运行时更改。 If it picks 12 for attack , every instance will have an attack of 12 . 如果它选择12 attack ,每个实例将有12attack Next time you started the program, you'd get another single randomly-selected value applied to each instance. 下次启动程序时,您将获得另一个随机选择的值应用于每个实例。 Maybe it would be 2 for every instance this time. 也许这次每个实例都是2

Also, class variables are substantively different from instance variables and you shouldn't use one to mimic the other. 此外,类变量与实例变量实质上不同,您不应使用其中一个来模仿另一个变量。

Overall, you should match the __init__() to the manner in which you intend to call and use the class. 总的来说,您应该将__init__()与您打算调用和使用该类的方式相匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在不创建类实例的情况下获取类实例属性? - Is there any way to get class instance attributes without creating class instance? 有没有更好的方法来创建一个是dict的子类但不区分大小写的类? - Is there a better way to create a class which is a subclass of `dict` but case-insensitive? 哪种导入方式在python中更好,以及对类对象的一些说明 - which way of importing is better in python and some clarifications on the class objects 哪种更好的继承方式? - Which is the better way to inheritance? 哪一种是比较 Python 中数字的更好方法? 为什么? - Which one is the better way to compare numbers in Python? And why? 是否有更好/更快/更清洁的方法来计算基于属性的 class 对象列表的重复项? - Is there a better/faster/cleaner way to count duplicates of a list of class objects based on their attributes? Python,引用基类中的子属性和方法(反之亦然)必须有更好的方法 - Python, Referencing child attributes and methods in base class (and vice versa) there has to be a better way 哪种方式更适合二分搜索? - Which way is better for Binary Search? 哪个是python中更好更快的方法? - Which is the better & faster way in python? 是否有更好的方法通过对象的属性之一拆分对象? - Is there a better way to split of objects by one of its attributes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM