重写方法 - 两个问题 [英] overriding methods - two questions

查看:66
本文介绍了重写方法 - 两个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里是问题的框架:


---在模块中,API的一部分---

class Basis(对象):

def foo(self,arg):

pass


- - 在用户自己的代码中---

class Child(Basis):

def foo(self,not,sure):

...

问题1:


鉴于API的用户可以选择覆盖foo(),怎么能

我控制他们使用的签名?在示例中,用户选择了

错误的参数,Python会抱怨,但是它描述的是

*重写*方法的sig,而不是父类。


有没有什么办法可以控制错误信息,让

用户明白他们正在使用foo()的签名不正确吗?


问题2:


说我在课堂上基础,做一个循环,我有一个Child对象列表。我想要为* * foo()方法的每一个运行foo()方法。即

用户已完成此操作:


类Sam(儿童):

...

* Sam没有定义foo()


class Judy(Child):

def foo(self,arg):

...

* Judy确实定义了foo()


Sam和Judy的实例已被放入列表中(在实例中)

of Basis。我希望Basis检测到Judy有foo()并运行它。


我可以使用必须由用户设置的标志来处理问题2.

类似于:

class Judy(孩子):

def __init __(自我):

self.pleaseCallFoo = true


现在,Basis可以检查那个var,然后再调用foo(),但这是丑陋的,这意味着用户可以更好地学习API。 br />

有什么想法吗?

/ d

解决方案

11月16日,11日:03:00,Donn Ingle< donn.in ... @ gmail.comwrote:




这是一个框架问题:


---在模块中,API的一部分---

class Basis(object):

def foo(self,arg):

通过


---用户自己的代码---

class Child(Basis):

def foo(self,not,sure):

...


Q uestion 1:

鉴于API的用户可以选择覆盖foo(),我怎么能控制他们使用的签名?b
在示例中,用户选择了

错误的参数,Python会抱怨,但是它描述的是

*重写*方法的sig,而不是父母班。



实际上,Python抱怨你的用户选择了不好的
参数名称。 ''not''是保留关键字。将它改为''naught''或

''knot''或''not_''并且Python会接受这个就好了。


是否这个是一个好主意或不是一个单独的问题。但是给出了
Python的哲学你就是人类,所以你必须知道你在做什么,b $ b正在做什么。 (这既是一个假设,也是一个指令),我不会因为你会找到很多语言机器来阻止它。


- 保罗

- Paul


Donn Ingle:


说我上课基础,做一个循环,我有一个Child对象列表。我想要为* * foo()方法的每一个运行foo()方法。



这可能会有所帮助(在旧的Python版本上):


>>类Sam:传递



....
< blockquote class =post_quotes>


>> class Judy:



.... def foo(self):pass

....


>> children = [Sam(),Judy(),Sam()]
适合儿童:hasattr(child," foo")



....

False

True

False


再见,

。熊友


实际上,Python正抱怨你的用户选择

参数名称。 ''not''是保留关键字。



我的例子很差,但我的实际测试代码并没有使用''不'。 Python简单地将
检查foo()的使用检查foo()的本地sig并且不会上升到

链。这是可以理解的,你的下一个答案或多或少都是我期待的。


Python的哲学你是人,所以你必须知道你在做什么? (这既是假设,也是指令),我不认为你会发现很多语言机制来阻止它。



是的。我想我希望能有一些聪明的伎俩。


/ d


Hi,
Here''s a framework for the questions:

--- In a module, part of an API ---
class Basis ( object ):
def foo ( self, arg ):
pass

--- In user''s own code ---
class Child ( Basis ):
def foo ( self, not, sure ):
...
Question 1:

Given that the user of the API can choose to override foo() or not, how can
I control the signature that they use? In the example the user has chosen
bad arguments and Python will complain, but it''s describing the sig of the
*overridden* method and not the one in the parent class.

Is there some way I can control the error message to make it clear to the
user that they are using the signature of foo() incorrectly?

Question 2:

Say I am in class Basis, doing a loop and I have a list of Child objects. I
want to run the foo() method for each one that *has* a foo() method. i.e.
user has done this:

class Sam ( Child ):
...
*Sam does not define foo()

class Judy ( Child ):
def foo ( self, arg ):
...
* Judy does define foo()

Instances of Sam and Judy have been put into the list (within the instance)
of Basis. I want Basis to detect that Judy has foo() and run it.

I can handle question 2 by using a flag that must be set by the user.
Something like:
class Judy ( child ):
def __init__( self ):
self.pleaseCallFoo = true

And now, Basis can check for that var and only then call foo(), but this is
ugly and means more for the user to learn API-wise.

Any ideas?
/d

解决方案

On Nov 16, 11:03 am, Donn Ingle <donn.in...@gmail.comwrote:

Hi,
Here''s a framework for the questions:

--- In a module, part of an API ---
class Basis ( object ):
def foo ( self, arg ):
pass

--- In user''s own code ---
class Child ( Basis ):
def foo ( self, not, sure ):
...

Question 1:

Given that the user of the API can choose to override foo() or not, how can
I control the signature that they use? In the example the user has chosen
bad arguments and Python will complain, but it''s describing the sig of the
*overridden* method and not the one in the parent class.

Actually, Python is complaining about your user''s poor choice of
argument names. ''not'' is a reserved keyword. Change it to ''naught'' or
''knot'' or ''not_'' and Python will accept this just fine.

Whether this is a good idea or not is a separate question. But given
Python''s philosophy of "you are the human, so you must know what you
are doing" (which is both an assumption and a directive), I don''t
think you will find much language machinery to prevent it.

-- Paul
-- Paul


Donn Ingle:

Say I am in class Basis, doing a loop and I have a list of Child objects. I
want to run the foo() method for each one that *has* a foo() method.

This may help (on an old Python version):

>>class Sam: pass

....

>>class Judy:

.... def foo(self): pass
....

>>children = [Sam(), Judy(), Sam()]
for child in children: hasattr(child, "foo")

....
False
True
False

Bye,
bearophile


Actually, Python is complaining about your user''s poor choice of

argument names. ''not'' is a reserved keyword.

My example was poor, but my actual test code did''t use ''not''. Python simply
checks the use of foo() to the local sig of foo() and does not go up the
chain. This is understandable and your next answer is more-or-less what I
was expecting.

Python''s philosophy of "you are the human, so you must know what you
are doing" (which is both an assumption and a directive), I don''t
think you will find much language machinery to prevent it.

Yeah. I guess I was hoping there''d be some clever trick to do it.

/d


这篇关于重写方法 - 两个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆