在__init__中分配值 [英] assigning values in __init__
问题描述
让我们说我正在制作一个游戏而且我有这个基类:
class字符(对象):
def __init __(self,name,stats):
self.name = name
self.strength = stats [0]
self .dexterity = stats [1]
self.intelligence = stats [2]
self.luck = stats [3]
这是将值分配给不同属性的好方法吗?
应该''stats''是列表/元组(像这样),还是应该做* stats?
我想要提前考虑什么时候我可能想要添加新属性,
我想确保这不会让个人感到疯狂
参数而不仅仅是一个列表。
或者可能有一些方法可以循环浏览两个列表(统计数据和
属性)并以这种方式分配它们?我想到了一个嵌套的
语句,但似乎没有用。
Let''s say I''m making a game and I have this base class:
class Character(object):
def __init__(self, name, stats):
self.name = name
self.strength = stats[0]
self.dexterity = stats[1]
self.intelligence = stats[2]
self.luck = stats[3]
Is this a good way to assign the values to the different attributes?
Should ''stats'' be a list/tuple (like this), or should I do *stats instead?
I''m trying to think ahead to when I might want to add new attributes,
and I want to make sure this doesn''t get crazy with individual
parameters instead of just the one list.
Or maybe there''s some way to loop through two lists (the stats and the
attributes) and assign them that way? I was thinking of a nested for
statement but that didn''t seem to work.
推荐答案
John Salerno写道:
John Salerno wrote:
让我们说我正在制作一个游戏而且我有这个基类:
class Character (对象):
def __init __(self,name,stats):
self.name = name
self.strength = stats [0]
self.dexterity = stats [1]
self.intelligence = stats [2]
self.luck = stats [3]
这是将值分配给不同属性的好方法吗?
应该''stats''是一个列表/元组(如这个),或者我应该做*统计数据吗?
我正在努力思考我何时想要添加新属性,
和我想确保个别
参数而不仅仅是一个列表并不会让我感到疯狂。
或者也许有一些方法可以遍历两个列表(统计数据和
属性)并以这种方式分配它们?我在想一个嵌套的
语句,但这似乎没有用。
Let''s say I''m making a game and I have this base class:
class Character(object):
def __init__(self, name, stats):
self.name = name
self.strength = stats[0]
self.dexterity = stats[1]
self.intelligence = stats[2]
self.luck = stats[3]
Is this a good way to assign the values to the different attributes?
Should ''stats'' be a list/tuple (like this), or should I do *stats instead?
I''m trying to think ahead to when I might want to add new attributes,
and I want to make sure this doesn''t get crazy with individual
parameters instead of just the one list.
Or maybe there''s some way to loop through two lists (the stats and the
attributes) and assign them that way? I was thinking of a nested for
statement but that didn''t seem to work.
听起来你应该做的就像是关键字参数
而不是。
class Character(object):
def __init __(self,name,** kwargs):
self.name = name
for key, kwargs.items中的值():
setattr(self,key,value)
z =字符(''name'',strength = 10,dexterity = 5,intelligence = 3,运气= 0)
现在您可以轻松引入新的关键字参数。
-Larry
Sounds like what you should be doing is something like keyword arguments
instead.
class Character(object):
def __init__(self, name, **kwargs):
self.name=name
for key, value in kwargs.items():
setattr(self, key, value)
z=Character(''name'', strength=10, dexterity=5, intelligence=3, luck=0)
Now you can easily introduce new keyword arguments.
-Larry
John Salerno写道:
John Salerno wrote:
让我们说我正在制作游戏而且我有这个基类:
class字符(对象):
def __init __(self,name,stats):
self.name = name
self.strength = stats [0]
self.dexterity = stats [1]
self.intelligence = stats [2]
self.luck = stats [3]
这是将值分配给不同属性的好方法吗?
应该''stats''是一个列表/元组(如这个),或者我应该做*统计数据?
Let''s say I''m making a game and I have this base class:
class Character(object):
def __init__(self, name, stats):
self.name = name
self.strength = stats[0]
self.dexterity = stats[1]
self.intelligence = stats[2]
self.luck = stats[3]
Is this a good way to assign the values to the different attributes?
Should ''stats'' be a list/tuple (like this), or should I do *stats instead?
如何 :
class字符(对象):
def __init __(self,name,** kwargs):
self.name = name
self .__ dict __。update(kwargs)
c =字符(柏拉图,力量= 10,运气= 12)
print getattr (c,strength)
print getattr(c,luck)
10
12 br />
How about:
class Character(object):
def __init__(self, name, **kwargs):
self.name = name
self.__dict__.update(kwargs)
c = Character( "Plato", strength=10, luck=12)
print getattr(c, "strength")
print getattr(c, "luck")
10
12
John Salerno写道:
John Salerno wrote:
让我们说我正在制作游戏我有这个基类:
class Character(object):
def __init __(self,name,stats):
self.name = name
self.strength = stats [0]
self.dexteri ty = stats [1]
self.intelligence = stats [2]
self.luck = stats [3]
这是将值分配给不同属性的好方法吗?
应该''stats''是列表/元组(就像这样),还是我应该做* stats?
我正在努力思考,当我想要添加新属性时,
我想确保个人不会发疯br />
参数而不仅仅是一个列表。
或者可能有一些方法可以遍历两个列表(统计数据和
属性)并以这种方式分配它们?我在想一个嵌套的
语句,但这似乎没有用。
Let''s say I''m making a game and I have this base class:
class Character(object):
def __init__(self, name, stats):
self.name = name
self.strength = stats[0]
self.dexterity = stats[1]
self.intelligence = stats[2]
self.luck = stats[3]
Is this a good way to assign the values to the different attributes?
Should ''stats'' be a list/tuple (like this), or should I do *stats instead?
I''m trying to think ahead to when I might want to add new attributes,
and I want to make sure this doesn''t get crazy with individual
parameters instead of just the one list.
Or maybe there''s some way to loop through two lists (the stats and the
attributes) and assign them that way? I was thinking of a nested for
statement but that didn''t seem to work.
如果你的程序处理4元素元组,那么虽然你*可以*
在你的调用中使用* stats来传递每个元素作为单个
参数的元组,这不是真的必要。在不使用索引的情况下编写所需的
初始化的方法是:
class字符(对象):
def __init __(self,name,stats):
self.name = name
self.strength,self.dexterity,\
self.intelligence,self.luck = stats
问候
Steve
-
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC / Ltd http://www.holdenweb.com
Skype:holdenweb http://holdenweb.blogspot.com
最近的Ramblings http://del.icio.us/steve.holden
If your program deals with 4-element tuples then although you *could*
use *stats in your calls to pass each element of the tuple as a single
argument, that''s not really necessary. A way to write the
initializations you want without using indexing is:
class Character(object):
def __init__(self, name, stats):
self.name = name
self.strength, self.dexterity, \
self.intelligence, self.luck = stats
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
这篇关于在__init__中分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!