Python论坛  - 讨论区

标题:[python-chinese] 一个super的用法的问题

2006年07月11日 星期二 15:05

mouqx xu zsuxqm at gmail.com
Tue Jul 11 15:05:23 HKT 2006

>>> 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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月11日 星期二 15:14

limodou limodou at gmail.com
Tue Jul 11 15:14:03 HKT 2006

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月11日 星期二 15:17

mouqx xu zsuxqm at gmail.com
Tue Jul 11 15:17:12 HKT 2006

刚弄明白了。
我是想调用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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月11日 星期二 22:12

ygao ygao2004 at gmail.com
Tue Jul 11 22:12:14 HKT 2006

这就是文档写得不好的地方,令人难以理解,可以接受三种类型的参数,可是
只有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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月13日 星期四 13:34

Bing Yuan aceyuan at gmail.com
Thu Jul 13 13:34:41 HKT 2006

*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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月13日 星期四 14:44

limodou limodou at gmail.com
Thu Jul 13 14:44:29 HKT 2006

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号