相关疑难解决方法(0)

什么是在Python中拥有多个构造函数的干净,pythonic方式?

我无法找到明确的答案.AFAIK,你不能__init__在Python类中拥有多个函数.那么我该如何解决这个问题呢?

假设我有一个Cheese使用该number_of_holes属性调用的类.我怎样才能有两种创建奶酪对象的方法......

  1. 一个像这样的洞: parmesan = Cheese(num_holes = 15)
  2. 并且不带参数并且只是随机化number_of_holes属性:gouda = Cheese()

我只想到一种方法来做到这一点,但这似乎有点笨重:

class Cheese():
    def __init__(self, num_holes = 0):
        if (num_holes == 0):
            # randomize number_of_holes
        else:
            number_of_holes = num_holes
Run Code Online (Sandbox Code Playgroud)

你说什么?还有另外一种方法吗?

python constructor

660
推荐指数
9
解决办法
26万
查看次数

如何在Python中使用方法重载?

我试图在Python中实现方法重载:

class A:
    def stackoverflow(self):    
        print 'first method'
    def stackoverflow(self, i):
        print 'second method', i

ob=A()
ob.stackoverflow(2)
Run Code Online (Sandbox Code Playgroud)

但输出是second method 2; 类似的:

class A:
    def stackoverflow(self):    
        print 'first method'
    def stackoverflow(self, i):
        print 'second method', i

ob=A()
ob.stackoverflow()
Run Code Online (Sandbox Code Playgroud)

Traceback (most recent call last):
  File "my.py", line 9, in <module>
    ob.stackoverflow()
TypeError: stackoverflow() takes exactly 2 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)

我该如何工作?

python overloading class

147
推荐指数
13
解决办法
18万
查看次数

python中重载的函数?

是否有可能在Python中重载函数?在C#中,我会做类似的事情

void myfunction (int first, string second)
{
//some code
}
void myfunction (int first, string second , float third)
{
//some different code
}
Run Code Online (Sandbox Code Playgroud)

然后当我调用函数时,它会根据参数的数量区分两者.是否有可能在Python中做类似的事情?

python arguments overloading function

83
推荐指数
3
解决办法
9万
查看次数

python中一个类中具有相同名称的方法?

如何声明一些具有相同名称但在一个类中具有不同数量的参数或不同类型的方法?

我必须在这堂课中改变一下:

class MyClass:
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
    def my_method(self,parameter_A_that_Must_Be_String):
        print parameter_A_that_Must_Be_String

    def my_method(self,parameter_A_that_Must_Be_String,parameter_B_that_Must_Be_String):
        print parameter_A_that_Must_Be_String
        print parameter_B_that_Must_Be_String

    def my_method(self,parameter_A_that_Must_Be_String,parameter_A_that_Must_Be_Int):
        print parameter_A_that_Must_Be_String * parameter_A_that_Must_Be_Int
Run Code Online (Sandbox Code Playgroud)

python overloading

45
推荐指数
4
解决办法
7万
查看次数

如何在python类中检测重复的方法名称?

在编写单元测试时,我有时剪切并粘贴测试,不记得更改方法名称.这导致覆盖先前的测试,有效地隐藏它并阻止它运行.例如;

class WidgetTestCase(unittest.TestCase):

  def test_foo_should_do_some_behavior(self):
    self.assertEquals(42, self.widget.foo())

  def test_foo_should_do_some_behavior(self):
    self.widget.bar()
    self.assertEquals(314, self.widget.foo())
Run Code Online (Sandbox Code Playgroud)

在这种情况下,只会调用后一个测试.是否有一种以编程方式捕获此类错误的方法,而不是直接解析原始源代码?

python

21
推荐指数
3
解决办法
3625
查看次数

如何在python中使用不同的参数编写同名方法

我正在从Java背景学习Python(3.x).

我有一个python程序,我在其中创建一个personObject并将其添加到列表中.

p = Person("John")
list.addPerson(p)
Run Code Online (Sandbox Code Playgroud)

但是为了灵活性,我还希望能够直接在addPerson方法中声明它,如下所示:

list.addPerson("John")
Run Code Online (Sandbox Code Playgroud)

addPerson方法将能够区分我是否发送Person-object或String.

在Java中,我将创建两个单独的方法,如下所示:

void addPerson(Person p) {
    //Add person to list
}

void addPerson(String personName) {
    //Create Person object
    //Add person to list
}
Run Code Online (Sandbox Code Playgroud)

我无法在Python中找到如何做到这一点.我知道一个type()函数,我可以用它来检查参数是String还是Object.然而,这对我来说似乎很混乱.还有另一种方法吗?

编辑:

我想替代解决方法将是这样的(python):

def addPerson(self, person):
    //check if person is string
        //Create person object

    //Check that person is a Person instance
        //Do nothing

    //Add person to list
Run Code Online (Sandbox Code Playgroud)

但与Java中的重载解决方案相比,它看起来很混乱.

python methods function parameter-passing python-3.x

8
推荐指数
2
解决办法
9891
查看次数

异常处理总是很昂贵吗?

我一次被告知,对于像确定类型这样的操作的异常处理是不好的形式,因为异常总是在计算上很昂贵.尽管如此,我已经看过帖子(特别是与Python相关的帖子,例如回复帖子),建议使用异常处理来达到这个目的.

我想知道,如果要普遍避免抛出和捕获异常,因为它总是在计算上很昂贵,或者某些语言​​(如Python)是否更好地处理异常,并允许更自由地使用异常处理.

python exception-handling exception

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

在python中"重载"函数的最佳方法?

我想在python中做这样的事情:

def foo(x, y):
    # do something at position (x, y)

def foo(pos):
    foo(pos.x, pos.y)
Run Code Online (Sandbox Code Playgroud)

所以我想根据我提供的参数数量调用不同版本的foo.这当然不起作用,因为我重新定义了foo.

实现这一目标最优雅的方式是什么?我宁愿不使用命名参数.

python overloading

3
推荐指数
2
解决办法
1936
查看次数