小编use*_*234的帖子

自定义 __str__ 和 __repr__ 不起作用

我正在尝试转换输出,例如:

decrypt : <torfuncs.TorHop instance at 0x7f44babb8440>
Run Code Online (Sandbox Code Playgroud)

变成更具可读性的东西,结果应该是数组中的字符串。我已经定义了__str____repr__但即使有这些,我仍然得到上面的结果。我究竟做错了什么?

class TorCircuit():
    def __init__(self, sock, circid):
        self.hops = []
        self.circId = circid
        self.socket = sock
        self.tempX = 0
        self.packetSendCount = 0
        self.cookie = []

    def __str__(self, hop):    
        return 'hop #%d' %  self.hops

    def __repr__(self, hop):
        return 'hop #%d' %  self.hops

    def decrypt(self, relayCell):
        for hop in self.hops:
            print "decrypt :", str(hop)
            relayCell = hop.decrypt(relayCell)
            if relayCell[1]==0 and relayCell[2]==0:
                return relayCell
        return relayCell
Run Code Online (Sandbox Code Playgroud)

我试图打印出当前用于解密的跃点

编辑托霍普

class TorHop:

    def __str__(self): …
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
1
解决办法
3982
查看次数

产生随机数的概率为1

我正在使用目前使用以下代码的数字生成器

from random import randint
print(randint(0,1))
Run Code Online (Sandbox Code Playgroud)

虽然它工作,我需要生成0或1,但我需要生成器生成1 with a x (say 80%)概率

我怎么能在python中有效地实现这一点?

python

1
推荐指数
1
解决办法
96
查看次数

标签 统计

python ×2