关于正确使用__slots__的问题 [英] Quesion about the proper use of __slots__

查看:127
本文介绍了关于正确使用__slots__的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>>>战斗机:

....''''''小型男子手上的工艺只能伤害其他战士自己。''''''

.... def __init __(self,statsTuple =(50,5,0,(2,4),1)):

.... self.fuel = statsTuple [0]

.... self.life = statsTuple [1]

.... self.armor = statsTuple [2]

.... self.weapon = statsTuple [3]

.... self.bulk = statsTuple [4]

.... __slots__ =

[self.fuel,self.life,self.armor,self.weapon,self.bu lk]

....

< blockquote class =post_quotes>

ral = Fighter()
ral.rocks = 2
ral.rocks
2 ral.life






我正在阅读特殊方法,到了插槽

http:// docs。 python.org/ref/slots.html )并决定我应该使用

来节省程序中的内存,因为它必须支持非常

大量的fighers一次。它说,一旦你定义了插槽

然后你就不能添加任何未在插槽中列出的变量,而是从我刚刚做过的那个

示例部分,所以我在做什么有什么不对或者我读了

错了吗?

解决方案



Zefria写道:

class Fighter:......'''''''''''''''''''''''''''''''''''''''''''''''''''''
他们自己。''''''
... def __init __(self,statsTuple =(50,5,0,(2,4),1)):
.. .self.fuel = statsTuple [0]
... self.life = statsTuple [1]
... self.armor = statsTuple [2]
... self.weapon = statsTuple [3]
... self.bulk = statsTuple [4]
... __slots__ =
[self.fuel,self.life,self.armor,self.weapon,self .bu lk]
... ral =战斗机()
ral.rocks = 2
ral。岩石2 ral.life


5
我正在阅读特殊方法,到了插槽
http://docs.python.org/ref/slots.html )并决定我应该使用<这是为了节省程序中的内存,因为它必须同时支持非常大量的fighers。它说,一旦你定义插槽
然后你就不能添加插槽中没有列出的任何变量,而是从我刚刚做的那个
示例部分,所以我做错了什么或者我读了
错了吗?




似乎__slots__在你的情况下只是一个''局部变量''

函数__init__。我想你需要这样的东西:


class Test(对象):

__slots__ = [''attr1'',''attr2''] <


Zefria写道:

class Fighter:



旧式类已被弃用,尽可能使用新式类:


类战斗机(对象):

....

...''''''小型男子手艺,只能伤害自己的其他战士。''''''
... def __init __(self,statsTuple =(50,5,0,(2,4),1)):
... self.fuel = statsTuple [0]
... self.life = statsTuple [1]
... self.armor = statsTuple [2]
... self.weapon = statsTuple [3]
... self。 bulk = statsTuple [4]
... __slots__ =
[self.fuel,self.life,self.armor,self.weapon,self.bu lk]
...

(剪辑)

我正在阅读特殊方法,到了插槽
http://docs.python.org/ref/slots.html )并决定我应该使用
这可以节省程序中的内存,因为它必须同时支持非常大量的fighers。


过早优化是所有邪恶的根源。

它说,一旦你定义了插槽
那么你就不能添加插槽中没有列出的任何变量,但是我刚刚做过的那个示例部分,所以我做错了什么或者我读错了吗?




我认为它是:


级战斗机(物体):

__slots__ = [''燃料'',''生活'' ,''盔甲'',''武器'',''批量'',]

def __init __(自我,...):

#code here


-

bruno desthuilliers

python -c" print''@''。join([''''' '.join([w [:: - 1] for p in p.split(''。'')])for

p in''o****@xiludom.gro'' .split(''@'')])"


>>>战斗机(物体):

....'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''

.... __slots__ = [" fuel",life,armor,weapon,bulk]

.... def __init __(self,statsTuple =(50,5,0,(2,4),1)):

.... self.fuel = statsTuple [0]

.... self.life = statsTuple [1]

.... self.armor = statsTuple [2]

.... self.weapon = statsTuple [3]

.... self.bulk = statsTuple [4]

....

examp = Fighter()
examp.rock = 3
Traceback(最近一次调用最后一次):

文件" ;< stdin>",第1行,在?

AttributeError:''Fighter''对象没有属性''rock''




谢谢你们两个,我曾经尝试过把它变成一个类变量,并没有看到

。诀窍必须是制作一个从对象继承

的类。


另外,我一般都不做任何优化(如一个高中学生

学生项目经常遭受破坏,没有打扰过),但在这个特殊情况下,我期待着每个载体。这两支球队每支球队最多有150美元b


$ b。 >
另外,这个程序的目的是尽可能多地尝试特殊方法,所以我想我到目前为止还在进行中。


>>> class Fighter:
.... ''''''Small one man craft that can only harm other fighters on
their own.''''''
.... def __init__(self,statsTuple=(50,5,0,(2,4),1)):
.... self.fuel = statsTuple[0]
.... self.life = statsTuple[1]
.... self.armor = statsTuple[2]
.... self.weapon = statsTuple[3]
.... self.bulk = statsTuple[4]
.... __slots__ =
[self.fuel,self.life,self.armor,self.weapon,self.bu lk]
....

ral = Fighter()
ral.rocks = 2
ral.rocks 2 ral.life


5

I was reading the special methods, got to slots
(http://docs.python.org/ref/slots.html) and decided that i should use
this to save memory in the program because it''ll have to support very
large numbers of fighers at once. It says that once you define slots
then you can''t add any variables not listed in slots, but from that
example section I just did, so am I doing something wrong or did I read
it wrong?

解决方案


Zefria wrote:

class Fighter: ... ''''''Small one man craft that can only harm other fighters on
their own.''''''
... def __init__(self,statsTuple=(50,5,0,(2,4),1)):
... self.fuel = statsTuple[0]
... self.life = statsTuple[1]
... self.armor = statsTuple[2]
... self.weapon = statsTuple[3]
... self.bulk = statsTuple[4]
... __slots__ =
[self.fuel,self.life,self.armor,self.weapon,self.bu lk]
... ral = Fighter()
ral.rocks = 2
ral.rocks 2 ral.life


5

I was reading the special methods, got to slots
(http://docs.python.org/ref/slots.html) and decided that i should use
this to save memory in the program because it''ll have to support very
large numbers of fighers at once. It says that once you define slots
then you can''t add any variables not listed in slots, but from that
example section I just did, so am I doing something wrong or did I read
it wrong?



Seems that __slots__ in your case is nothing but a ''local variable'' to
the function __init__. I think you need something like this :

class Test(object):
__slots__ = [''attr1'',''attr2'']


Zefria wrote:

class Fighter:


Old-style classes are deprecated, use new-style class wherever possible:

class Fighter(object):
....
... ''''''Small one man craft that can only harm other fighters on
their own.''''''
... def __init__(self,statsTuple=(50,5,0,(2,4),1)):
... self.fuel = statsTuple[0]
... self.life = statsTuple[1]
... self.armor = statsTuple[2]
... self.weapon = statsTuple[3]
... self.bulk = statsTuple[4]
... __slots__ =
[self.fuel,self.life,self.armor,self.weapon,self.bu lk]
...
(snip)

I was reading the special methods, got to slots
(http://docs.python.org/ref/slots.html) and decided that i should use
this to save memory in the program because it''ll have to support very
large numbers of fighers at once.
"premature optimization is the root of all evil".
It says that once you define slots
then you can''t add any variables not listed in slots, but from that
example section I just did, so am I doing something wrong or did I read
it wrong?



I think it''s:

class Fighter(object):
__slots__ = [''fuel'', ''life'', ''armor'', ''weapon'', ''bulk'',]
def __init__(self, ...):
# code here

--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


>>> class Fighter(object):
.... ''''''Small one man craft that can only harm other fighters on
their own.''''''
.... __slots__ = ["fuel","life","armor","weapon","bulk"]
.... def __init__(self,statsTuple=(50,5,0,(2,4),1)):
.... self.fuel = statsTuple[0]
.... self.life = statsTuple[1]
.... self.armor = statsTuple[2]
.... self.weapon = statsTuple[3]
.... self.bulk = statsTuple[4]
....

examp = Fighter()
examp.rock = 3 Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: ''Fighter'' object has no attribute ''rock''



Thank you both, I had tried making it a class variable and didn''t see
anything. The trick must have been in making a class that inheriets
from object.

Also, I don''t generally do any optimization at all yet (as a highschool
student projects get trashed often enough no to bother over), but in
this special case I''m expecting each "carrier" to have up to 150
fighters, and 3 to 5 carriers for each of the two teams, which comes
out to be quite large.

Additionally, this program''s purpose was to experiment with as many of
the special methods as possible, so I suppose I''m on track so far.


这篇关于关于正确使用__slots__的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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