2006年07月11日 星期二 15:05
>>> class F(object): ... def fun(self): ... print "father" ... >>> class S(F): ... def fun(self): ... print "son" ... >>> class SS(S): ... def fun(self): ... super(S,SS).fun() ... >>> SS().fun() Traceback (most recent call last): File "", line 1, in ? File " ", line 3, in fun TypeError: unbound method fun() must be called with SS instance as first argument (got nothing instead) 文档里是这么说的: *super*( type[, object-or-type]) Return the superclass of type. If the second argument is omitted the super object returned is unbound. If the second argument is an object, isinstance(obj, type) must be true. If the second argument is a type, issubclass(type2, type) must be true. super()only works for new-style classes. 第二个参数可以是type,那为什么我那样调用就错了呢? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060711/94c73f5d/attachment.htm
2006年07月11日 星期二 15:14
On 7/11/06, mouqx xu <zsuxqm at gmail.com> wrote: > > >>> class F(object): > ... def fun(self): > ... print "father" > ... > >>> class S(F): > ... def fun(self): > ... print "son" > ... > >>> class SS(S): > ... def fun(self): > ... super(S,SS).fun() > ... > >>> SS().fun() > Traceback (most recent call last): > File "", line 1, in ? > File "", line 3, in fun > TypeError: unbound method fun() must be called with SS instance as first > argument (got nothing instead) > > 文档里是这么说的: > *super*( type[, object-or-type]) Return the superclass of type. If the > second argument is omitted the super object returned is unbound. If the > second argument is an object, isinstance(obj, type) must be true. If the > second argument is a type, issubclass(type2, type) must be true. super()only works for new-style classes. > 第二个参数可以是type,那为什么我那样调用就错了呢? > > > 如果你要调用父类的方法,一般是这样: super(SS, self).fun() 而你的传入对于super是没有错的,但对于fun()出了错,因为对象的方法第一个参数需要是实例,而你的用法并没有把实例传进去。 -- I like python! My Blog: http://www.donews.net/limodou My Django Site: http://www.djangocn.org NewEdit Maillist: http://groups.google.com/group/NewEdit -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060711/5cb483d5/attachment.html
2006年07月11日 星期二 15:17
刚弄明白了。 我是想调用F类里的fun,其实只需要这样调用:super(S, self).fun(),就可以调用S的父类的函数了。 On 7/11/06, limodou <limodou at gmail.com> wrote: > > On 7/11/06, mouqx xu <zsuxqm at gmail.com> wrote: > > > > >>> class F(object): > > ... def fun(self): > > ... print "father" > > ... > > >>> class S(F): > > ... def fun(self): > > ... print "son" > > ... > > >>> class SS(S): > > ... def fun(self): > > ... super(S,SS).fun() > > ... > > >>> SS().fun() > > Traceback (most recent call last): > > File "", line 1, in ? > > File "", line 3, in fun > > TypeError: unbound method fun() must be called with SS instance as first > > argument (got nothing instead) > > > > 文档里是这么说的: > > *super*( type[, object-or-type]) Return the superclass of type. If the > > second argument is omitted the super object returned is unbound. If the > > second argument is an object, isinstance(obj, type) must be true. If the > > second argument is a type, issubclass(type2, type) must be true. super()only works for new-style classes. > > 第二个参数可以是type,那为什么我那样调用就错了呢? > > > > > > 如果你要调用父类的方法,一般是这样: > > super(SS, self).fun() > > 而你的传入对于super是没有错的,但对于fun()出了错,因为对象的方法第一个参数需要是实例,而你的用法并没有把实例传进去。 > > -- > I like python! > My Blog: http://www.donews.net/limodou > My Django Site: http://www.djangocn.org > NewEdit Maillist: http://groups.google.com/group/NewEdit > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060711/43adc285/attachment.htm
2006年07月11日 星期二 22:12
这就是文档写得不好的地方,令人难以理解,可以接受三种类型的参数,可是 只有super(type,obj)写得明确,其它的根本就不知道怎么用。 On 7/11/06, mouqx xu <zsuxqm at gmail.com> wrote: > > 刚弄明白了。 > 我是想调用F类里的fun,其实只需要这样调用:super(S, self).fun(),就可以调用S的父类的函数了。 > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > On 7/11/06, mouqx xu <zsuxqm at gmail.com> wrote: > > > > >>> class F(object): > > ... def fun(self): > > ... print "father" > > ... > > >>> class S(F): > > ... def fun(self): > > ... print "son" > > ... > > >>> class SS(S): > > ... def fun(self): > > ... super(S,SS).fun() > > ... > > >>> SS().fun() > > Traceback (most recent call last): > > File "", line 1, in ? > > File "", line 3, in fun > > TypeError: unbound method fun() must be called with SS instance as first > > argument (got nothing instead) > > > > 文档里是这么说的: > > *super*( type[, object-or-type]) Return the superclass of type. If > > the second argument is omitted the super object returned is unbound. If the > > second argument is an object, isinstance(obj, type) must be true. If the > > second argument is a type, issubclass(type2, type) must be true. super()only works for new-style classes. > > 第二个参数可以是type,那为什么我那样调用就错了呢? > > > > > > > > 如果你要调用父类的方法,一般是这样: > > super(SS, self).fun() > > 而你的传入对于super是没有错的,但对于fun()出了错,因为对象的方法第一个参数需要是实例,而你的用法并没有把实例传进去。 > > -- > I like python! > My Blog: http://www.donews.net/limodou > My Django Site: http://www.djangocn.org > NewEdit Maillist: http://groups.google.com/group/NewEdit > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- ※※※※※※※※※※※※※※※※※※※※※※※※ My blog: http://blog.donews.com/ygao Forum http://groups.google.com/group/python_study ※※※※※※※※※※※※※※※※※※※※※※※※ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060711/33db3d2e/attachment-0001.htm
2006年07月13日 星期四 13:34
*super*( type[, object-or-type]) Return the superclass of type. If the second argument is omitted the super object returned is unbound. If the second argument is an object, isinstance(obj, type) must be true. If the second argument is a type, issubclass(type2, type) must be true. super()only works for new-style classes. 文档中说如果省略第二个参数则返回的super对象是unbound的,能否解释一下这里的unbound对象是什么意思吗? On 7/11/06, ygao <ygao2004 at gmail.com> wrote: > > 这就是文档写得不好的地方,令人难以理解,可以接受三种类型的参数,可是 > 只有super(type,obj)写得明确,其它的根本就不知道怎么用。 > > > On 7/11/06, mouqx xu <zsuxqm at gmail.com> wrote: > > > > 刚弄明白了。 > > 我是想调用F类里的fun,其实只需要这样调用:super(S, self).fun(),就可以调用S的父类的函数了。 > > > > On 7/11/06, limodou < limodou at gmail.com> wrote: > > > > > On 7/11/06, mouqx xu <zsuxqm at gmail.com> wrote: > > > > > > >>> class F(object): > > > ... def fun(self): > > > ... print "father" > > > ... > > > >>> class S(F): > > > ... def fun(self): > > > ... print "son" > > > ... > > > >>> class SS(S): > > > ... def fun(self): > > > ... super(S,SS).fun() > > > ... > > > >>> SS().fun() > > > Traceback (most recent call last): > > > File "", line 1, in ? > > > File "", line 3, in fun > > > TypeError: unbound method fun() must be called with SS instance as > > > first argument (got nothing instead) > > > > > > 文档里是这么说的: > > > *super*( type[, object-or-type]) Return the superclass of type. If > > > the second argument is omitted the super object returned is unbound. If the > > > second argument is an object, isinstance(obj, type) must be true. If > > > the second argument is a type, issubclass(type2, type) must be true. > > > super() only works for new-style classes. 第二个参数可以是type,那为什么我那样调用就错了呢? > > > > > > > > > > > > 如果你要调用父类的方法,一般是这样: > > > > super(SS, self).fun() > > > > 而你的传入对于super是没有错的,但对于fun()出了错,因为对象的方法第一个参数需要是实例,而你的用法并没有把实例传进去。 > > > > -- > > I like python! > > My Blog: http://www.donews.net/limodou > > My Django Site: http://www.djangocn.org > > NewEdit Maillist: http://groups.google.com/group/NewEdit > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > -- > ※※※※※※※※※※※※※※※※※※※※※※※※ > My blog: http://blog.donews.com/ygao > Forum http://groups.google.com/group/python_study > ※※※※※※※※※※※※※※※※※※※※※※※※ > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060713/6096e18b/attachment.html
2006年07月13日 星期四 14:44
On 7/13/06, Bing Yuan <aceyuan at gmail.com> wrote: > > *super*( type[, object-or-type]) Return the superclass of type. If the > second argument is omitted the super object returned is unbound. If the > second argument is an object, isinstance(obj, type) must be true. If the > second argument is a type, issubclass(type2, type) must be true. super()only works for new-style classes. > 文档中说如果省略第二个参数则返回的super对象是unbound的,能否解释一下这里的unbound对象是什么意思吗? > > http://bbs.chinaunix.net/viewthread.php?tid=789006&extra;=page%3D1 -- I like python! My Blog: http://www.donews.net/limodou My Django Site: http://www.djangocn.org NewEdit Maillist: http://groups.google.com/group/NewEdit -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060713/1021361f/attachment.htm
Zeuux © 2025
京ICP备05028076号