English 中文(简体)
如何使用同一职能,以不平等的论据称呼两个职能?
原标题:How to call a two functions with unequal arguments using same function call?
  • 时间:2012-01-13 06:08:00
  •  标签:
  • python

我有两个定义:

档案:

class x:

    def p(self, a, b) :
        # ...

档案和材料:

class y:

    def p(self, a, b, c) :
        # [...]

现在,我从另一个档案中说这些职能。 带有档案名称的指挥线投入(此处为x和 y)载于<条码>。

z.py:

from x import  *
from y import  *

class z:

    cust.p(a, b, c)

x (or y) is being passed as the command line inputs to file z.py.
cust is the class variable of class z.

Now when cust = x object I get an error: takes exactly 2 arguments (3 given).

但当<代码>cust = y表示反对时,该编码即行不通。

如何消除这一错误?

我不想修改x.py和 y.py档案的功能定义,因为有许多档案。

如何修改该守则,以便这两个职能都从同一个职能中称作,而不修改职能定义?

最佳回答

单包。

x.p = lambda self, a, b, c=None: x.p(self, a, b)
问题回答

改变您的方法,使第三个参数在......中成为任择参数。

class x:
  def p(a, b, c=None):
    """ c will be ignored. """
    print a, b


class y:
  def p(a, b, c=1234):
    """ c will be used, with a default of 1234. """
    print a, b, c

x().p(1, 2)
x().p(1, 2, 123)
y().p(1, 2)
y().p(1, 2, 123)

根据我对守则和你的规格(坦率地说,这并不十分清楚)的理解,你希望同时从这两个档案中公布这些功能,而且你有这一可变的“<条码>>>。 你们通过这些方法将他们称为方法。

在你具体说明“<条码><>cust”之前,我无法做很多帮助。 我也不知道如何同时使用<代码>x和y。 http://em>might。 是你们想要的:

#file x.py
class x:
    def p(self, a, b):
        ...

#file y.py
class y:
    def p(self, a, b, c):
        ...

#file z.py
from x import *
from y import *
my_x = x()
my_y = y()
a = ...
b = ...
my_x.p(a, b)
my_y.p(a, b, c)

我希望你能更具体地了解你想要做些什么,以便我能够提供帮助。

Quick and dirty answer is:

try:
   cust.p(a,b,c)
except TypeError:
   cust.p(a,b)

否认:在如何界定原始问题方面,存在许多问题,因此,这种做法似乎不会使情况恶化。

如果您能够改变要求使用<代码>*args或**kwargs的职能,则您必须增加特殊案例,以了解物体的类别:

if isinstance(cust, x):
    cust.p(a, b)
elif isinstance(cust, y):
    cust.p(a, b, c)

http://docs.python.org/library/tuss.html





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...