开发者

not enough arguments passed to __init__()

What is the error below? Also, is there a better way to implement the following classes?

     #!/usr/bin/python

     class Datacenters:
        def __init__(self,name,location,cpu,mem):
           self.name=name
           self.location=location
           self.cpu=cpu
           self.mem=mem

        def  getparam(self):
           return self.name,self.location ,self.cpu,self.mem

        def  getname(self):
           return self.name

     class WS(Datacenters):
        def __init__(self,name,location,cpu,mem,obj):
开发者_StackOverflow社区           #datacentername = Datacenters.__init__(self) #To which data center it is associated
           self.dcname =obj.name #To which data center it is associated

           Datacenters.__init__(obj,name,location,cpu,mem)

        def getparam(self,obj):
           self.name,self.location ,self.cpu,self.mem = obj.getparam()
           print self.dcname
           #return self.name,self.location ,self.cpu,self.mem,obj.name

        def  getwsname(self):
           return self.name

     class Pcs(WS):
        def __init__(self,name,location,cpu,mem,obj):
           self.wsname = obj.getwsname() #To which WS it is  associated
           WS.__init__(obj,name,location,cpu,mem)

        def  getparam(self,obj):
           print obj.getparam()
           print self.wsname


     a = Datacenters("dc1","Bl1",20,30)
     print a.getparam()
     b = WS("WS1","Bl1",21,31,a)
     print b.getparam(a)
     c = Pcs("PC1","Bl1",20,30,b)
     #print c.getparam(b)

output:

   Press ENTER or type command to continue 
   ('dc1', 'Bl1', 20, 30)
   dc1
   None
   Traceback (most recent call last):
   File "class1.py", line 45, in <module>
   c = Pcs("PC1","Bl1",20,30,b)
   File "class1.py", line 34, in __init__
   WS.__init__(obj,name,location,cpu,mem)
   TypeError: __init__() takes exactly 6 arguments (5 given)


The error is that you pass in five arguments, but the __init__ needs six. Count them:

def __init__(self,name,location,cpu,mem,obj):

Six arguments. You call it like so:

WS.__init__(obj,name,location,cpu,mem)

Five arguments. The first one, self is missing. What you should ask yourself is why you don't have to pass in six arguments all the time.

And that is because self is passed in automatically when you call the method on an instance. However, in this case you don't call it on an instance, you call it directly on the class. There is of course no need to do so in this case, the correct syntax is:

WS(obj,name,location,cpu,mem)

As you indeed above note works further up.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜