小编yay*_*ayu的帖子

基础程序无法加载(Haskell)

我正在尝试加载以下程序

gcd a b = if b == 0
         then a
      else gcd b (rem a b)
Run Code Online (Sandbox Code Playgroud)

但是我得到了错误

Prelude> :l euclidean.hs 
[1 of 1] Compiling Main             ( euclidean.hs, interpreted )

euclidean.hs:3:8:
    Ambiguous occurrence `gcd'
    It could refer to either `Main.gcd', defined at euclidean.hs:1:0
                          or `Prelude.gcd', imported from Prelude
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)

我从改变函数名gcdmain和我

Couldn't match expected type `IO t'
           against inferred type `a -> a -> a'
    In the expression: main
    When checking the type of …
Run Code Online (Sandbox Code Playgroud)

haskell

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

理解应该绑定到对象实例的类范围的变量

这是一个为玩家分配符号的类。它应该接受一个移动并将该移动添加到玩家现有的移动存储库中。

class Player:
   ...:     positions = []
   ...:     def __init__(self,symbol):
   ...:         self.symbol = symbol
   ...:     def move(self,position):
   ...:         self.position = position
   ...:         self.positions.append(self.position)
Run Code Online (Sandbox Code Playgroud)

我的问题是位置的行为是“全局”的,因为它不与对象实例绑定,以证明:

>>>a = Player('x')
>>>b = Player('y')
>>>a.move(1)
>>>b.positions
[1]
Run Code Online (Sandbox Code Playgroud)

python oop

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

python格式化字符串以忽略缩进空格

我有一个实例属性为a,b,c的类.我使用textwrap但它不起作用.

 def __str__(self):
    import textwrap.dedent
    return textwrap.dedent(
    """#{0}
    {1}
    {2}
    """.format(self.a,self.b,self.c)
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用,我得到的输出就像

a
        b
        c
Run Code Online (Sandbox Code Playgroud)

python string formatting

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

在继承时我需要初始化父类吗?

如果我从一个类继承并且没有更改方法中的任何内容,是否需要使用super来从父类初始化该方法?

class A:

   def __init__(self):
       self.html = requests.get("example.com").text


class B(A):

    def __init__(self):
        # is this needed?
        super(B, self).__init__()

    def new_method(self):
        print self.html
Run Code Online (Sandbox Code Playgroud)

python super

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

Python可迭代和上下文管理器

我想要这样的行为:

with A() as f:
    for x in f:
        do_something(f)
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?

class A:
    def __enter__(self):
        print "Entering context"

    def __iter__(self):
        for x in ["some","list"]:
            yield x

    def __exit__(self):
        print "Deleting context"
Run Code Online (Sandbox Code Playgroud)

python contextmanager

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

如何从 github 中删除重命名的文件夹

我在 github 上有一个文件夹,folderfolder1在几次提交前将其重命名为。我现在看到 和folderfolder1出现在我的存储库中。如何删除旧文件夹。我尝试过 git rm 和 git mv 但它们不起作用,因为它们folder不再存在于我的目录树中。

git github

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

用多个值替换列表的元素

如果我有

l = ['a','b','x','f']
Run Code Online (Sandbox Code Playgroud)

我想替换'x'sub_list = ['c','d','e']

做这个的最好方式是什么?我有,

l = l[:index_x] + sub_list + l[index_x+1:]
Run Code Online (Sandbox Code Playgroud)

python

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

python请求:重试直到收到有效响应

我想知道是否存在一种常见的模式,可以重试请求一定次数(由于服务器错误或网络故障而导致失败)。我想出了这个,我愿意在那里找到更好的实现。

cnt=0
while cnt < 3:
    try:
        response = requests.get(uri)
        if response.status_code == requests.codes.ok:
            return json.loads(response.text)
    except requests.exceptions.RequestException:
        continue
    cnt += 1
return False
Run Code Online (Sandbox Code Playgroud)

python exception python-requests

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