两个self的问题

fnzh0003 2014-07-18 08:32:56
class super:
def ___init__(self,x):
.....some code

class sub(super):
def __init__(self,x,y):
super.__init__(self,x)
.....some cod

请问这里的sub 类,def __init__(self,x,y) 的self 是sub的实例,super.__init__(self,x) 的self是super的实例,他们所指是不同的吧?
如果不同,在sub类执行的时候,super的self实例不同于sub的实例,这不会出问题吗?
...全文
222 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsjfdjoijvtghu 2014-07-20
  • 打赏
  • 举报
回复
继承类,应该也继承了类的method,大概这么觉得
angel_su 2014-07-18
  • 打赏
  • 举报
回复
super的一般用法是调用得到一个实例,不是引用吧... #parent.__init__(self, x) super().__init__(x) python 2里,super参数不能缺省,类要是新式类继承object class parent(object): ... super(child, self).__init__(x) ...
计算机器 2014-07-18
  • 打赏
  • 举报
回复
引用 6 楼 xmnathan 的回复:
[quote=引用 5 楼 shrenk 的回复:] 我用的是Python 3.4,确实报这个错误。 我把这句“super.__init__(self, x)” 改为了“parent.__init__(self, x)" 报错:object.__init__() takes no parameters 为啥调用了object的构造呢

class parent:
    def __init__(self, x):#刚才 漏敲了个i  所以调用了默认的class基类object
        print 'p'
        pass
class child(parent):
    def __init__(self, x, y):
        parent.__init__(self, x)
        print 'c'
 
a = child(1,1)
python所有的类型都是从object继承来的[/quote] 是我敲错了,唉。 这下对了:
class parent:
    def __init__(self, x):
        print(x)

class child(parent):
    def __init__(self, x, y):
        parent.__init__(self, x)
        

a = child(1,1)
对楼主来说,调用父类的构造的时候,需要使用parent.__init__(self, x)才对,这样参数中的self才是父类的类型,就不会有楼主的问题了。 如果使用super,应该这样调用:super().__init__(x),不知道我说的对不对。
  • 打赏
  • 举报
回复
引用 5 楼 shrenk 的回复:
我用的是Python 3.4,确实报这个错误。 我把这句“super.__init__(self, x)” 改为了“parent.__init__(self, x)" 报错:object.__init__() takes no parameters 为啥调用了object的构造呢

class parent:
    def __init__(self, x):#刚才 漏敲了个i  所以调用了默认的class基类object
        print 'p'
        pass
class child(parent):
    def __init__(self, x, y):
        parent.__init__(self, x)
        print 'c'
 
a = child(1,1)
python所有的类型都是从object继承来的
计算机器 2014-07-18
  • 打赏
  • 举报
回复
引用 4 楼 xmnathan 的回复:
[quote=引用 3 楼 shrenk 的回复:]
class parent:
    def __int__(self, x):
        print('ss')
        pass
class child(parent):
    def __init__(self, x, y):
        super.__init__(self, x)

child(1,1)
报错: super.__init__(self, x) TypeError: descriptor '__init__' requires a 'super' object but received a 'child' 调用父类的构造函数要求传入一个parent对象
你这代码都敲错了吧 [/code] [/quote] 我用的是Python 3.4,确实报这个错误。 我把这句“super.__init__(self, x)” 改为了“parent.__init__(self, x)" 报错:object.__init__() takes no parameters 为啥调用了object的构造呢
  • 打赏
  • 举报
回复
引用 3 楼 shrenk 的回复:
class parent:
    def __int__(self, x):
        print('ss')
        pass
class child(parent):
    def __init__(self, x, y):
        super.__init__(self, x)

child(1,1)
报错: super.__init__(self, x) TypeError: descriptor '__init__' requires a 'super' object but received a 'child' 调用父类的构造函数要求传入一个parent对象
你这代码都敲错了吧

class parent:
    def __int__(self, x):
        print 'p'
        pass
class child(parent):
    def __init__(self, x, y):
        parent.__int__(self, x)
        print 'c'
 
a = child(1,1)
执行结果

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
p
c
>>> 
计算机器 2014-07-18
  • 打赏
  • 举报
回复
class parent:
    def __int__(self, x):
        print('ss')
        pass
class child(parent):
    def __init__(self, x, y):
        super.__init__(self, x)

child(1,1)
报错: super.__init__(self, x) TypeError: descriptor '__init__' requires a 'super' object but received a 'child' 调用父类的构造函数要求传入一个parent对象
  • 打赏
  • 举报
回复
这个sub类是super类的派生类,所以可以调用基类的方法 self是当前实例指针,相当于C++中的this,这里只是一个访问参数而已,调用派生类实例当然可以用来调用基类成员函数 楼主应该先看看python类的继承
lieyingshengbao 2014-07-18
  • 打赏
  • 举报
回复
同样新手,说下个人理解: 1. 两个self肯定不一样 2. 不会有问题 在构造不同对象时,用不同的类即可、 如: 基类: xx=super(param) 派生类: xx=sub(p1,p2)

37,717

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧