我无法找到明确的答案.AFAIK,你不能__init__在Python类中拥有多个函数.那么我该如何解决这个问题呢?
假设我有一个Cheese使用该number_of_holes属性调用的类.我怎样才能有两种创建奶酪对象的方法......
parmesan = Cheese(num_holes = 15)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中实现方法重载:
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中重载函数?在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中做类似的事情?
如何声明一些具有相同名称但在一个类中具有不同数量的参数或不同类型的方法?
我必须在这堂课中改变一下:
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) 在编写单元测试时,我有时剪切并粘贴测试,不记得更改方法名称.这导致覆盖先前的测试,有效地隐藏它并阻止它运行.例如;
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)
在这种情况下,只会调用后一个测试.是否有一种以编程方式捕获此类错误的方法,而不是直接解析原始源代码?
我正在从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相关的帖子,例如回复此帖子),建议使用异常处理来达到这个目的.
我想知道,如果要普遍避免抛出和捕获异常,因为它总是在计算上很昂贵,或者某些语言(如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 ×8
overloading ×4
function ×2
arguments ×1
class ×1
constructor ×1
exception ×1
methods ×1
python-3.x ×1