从Python数组中提取值并在另一个def中使用
我已经更新了这个代码,修改建议的沉默。我想访问位于数据包数组[]和/或下面的数组类型[]中的hands_count的值从Python数组中提取值并在另一个def中使用,python,Python,我已经更新了这个代码,修改建议的沉默。我想访问位于数据包数组[]和/或下面的数组类型[]中的hands_count的值 def history2packets(self, history, game_id, cache): game_index = 0 player_list_index = 7 packets = [] for event in history: self.event = event type = event[0]
def history2packets(self, history, game_id, cache):
game_index = 0
player_list_index = 7
packets = []
for event in history:
self.event = event
type = event[0]
if type == "game":
(type, level, hand_serial, hands_count, time, variant, betting_structure, player_list, dealer, serial2chips) = event
if len(serial2chips) > 1:
nochips = 0
for (serial, chips) in serial2chips.iteritems():
if serial == 'values':
continue
packets.append(PacketPokerPlayerChips(game_id = game_id,
serial = serial,
bet = nochips,
money = chips))
packets.append(PacketPokerInGame(game_id = game_id,
players = player_list))
if self.previous_dealer == dealer:
previous_dealer = -1
else:
previous_dealer = self.previous_dealer
packets.append(PacketPokerDealer(game_id = game_id,
dealer = dealer,
previous_dealer = previous_dealer))
self.previous_dealer = dealer
packets.append(PacketPokerStart(game_id = game_id,
hand_serial = hand_serial,
hands_count = hands_count,
time = time,
level = level))
并在另一个函数中使用该值,如下所示:
def autoDeal(self):
self.cancelDealTimeout()
if not self.allReadyToPlay():
for player in self.game.playersAll():
if player.getUserData()['ready'] == False:
for avatar in self.avatar_collection.get(player.serial):
if self.factory.verbose > 1:
self.message("Player %d marked as having a bugous PokerProcessingHand protocol" % player.serial)
avatar.bugous_processing_hand = True
if self.event[2] != 0:
self.beginTurn()
self.update()
else:
reactor.callLater(10, self.beginTurn)
self.update()
我仍然得到一个错误,但一个不同的
2015-02-19 22:10:11-0500 [-] Unhandled Error
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/twisted/application/app.py", line 445, in startReactor
self.config, oldstdout, oldstderr, self.profiler, reactor)
File "/usr/lib/python2.6/dist-packages/twisted/application/app.py", line 348, in runReactorWithLogging
reactor.run()
File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 1170, in run
self.mainLoop()
File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 1179, in mainLoop
self.runUntilCurrent()
--- <exception caught here> ---
File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 778, in runUntilCurrent
call.func(*call.args, **call.kw)
File "/usr/lib/python2.6/dist-packages/pokernetwork/pokertable.py", line 738, in autoDeal
if self.event[2] != 0:
exceptions.AttributeError: PokerTable instance has no attribute 'event'
2015-02-19 22:10:11-0500[-]未处理的错误
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.6/dist packages/twisted/application/app.py”,第445行,在StartRector中
self.config、oldsdout、oldsderr、self.profiler、reactor)
文件“/usr/lib/python2.6/dist-packages/twisted/application/app.py”,第348行,位于runReactorWithLogging中
反应堆运行()
文件“/usr/lib/python2.6/dist-packages/twisted/internet/base.py”,第1170行,运行中
self.mainLoop()
文件“/usr/lib/python2.6/dist-packages/twisted/internet/base.py”,第1179行,在mainLoop中
self.rununtlcurrent()
--- ---
文件“/usr/lib/python2.6/dist packages/twisted/internet/base.py”,第778行,在rununtlcurrent中
call.func(*call.args,**call.kw)
文件“/usr/lib/python2.6/dist packages/pokernetwork/pokertable.py”,第738行,在autoDeal中
如果自我事件[2]!=0:
exceptions.AttributeError:PokerTable实例没有属性“event”
这两个函数都在同一个模块和类中。还有什么问题吗?变量
事件
在History2Packages
中定义,但它不是全局变量。因此,当您在不同的函数中引用具有此名称的变量时,会引发名称错误
变量事件
在方法History2Packages
的局部范围内定义。如果在另一个作用域(例如函数或方法)中引用它,它将引发名称错误
,因为它未在该作用域中定义
解决这一问题的一个快速方法是在两种方法的顶部都加上一行字,上面写着globalevent
,但这通常是不受欢迎的。另一种方法是从history2packets
返回event
,并生成autoDeal
autoDeal(self,event)
。这意味着您可以拨打:
e = history2packets(...)
# 5 lines later
autodeal(e)
另一种方法是假设history2packets
和autoDeal
是同一类的两种方法。在History2Packages
中的此处修改dome代码:
packets = []
for event in history:
self.event = event
type = event[0]
然后,进行autoDeal
def autoDeal(self):
self.cancelDealTimeout()
if not self.allReadyToPlay():
# Some lines later
if self.event[2] != 0: # This is the changed line
# Same as before
见例。一旦你开始工作,我强烈建议你把这个交给我真的希望这只是一件有趣的事情…@jonrsharpe谢谢你的链接。如果我能让它工作,我会把它交给codeview。请再看一看,我已经实现了你的建议,但仍然有一个问题。我真的很想理解这一点,你的答案似乎很全面,但到目前为止,我对你提供的任何场景都没有把握。这意味着它们不在同一个类的实例中。然后您可能应该在
history2packets
的顶部编写global event
解决方案,这样event
就在全局范围内。我无法使用history2packets()方法放弃hands\u count的值。我相信在autoDeal()需要的时候,hands\u count并没有立即填充。然而,我能够通过在另一个模块中全局化另外两个变量来实现第一笔交易的延迟。因此,我没有使用hands_count变量作为延迟的关键,而是使用服务器状态变量的变化(例如,当游戏服务器从注册游戏变为运行游戏时)。我使用名为old_state的全球化变量作为autoDeal()中的测试,以使其工作。