1

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):
           #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)
Kevin
  • 74,910
  • 12
  • 133
  • 166
Rajeev
  • 44,985
  • 76
  • 186
  • 285
  • Have a look here: http://stackoverflow.com/questions/904036/chain-calling-parent-constructors-in-python – sje397 Dec 19 '10 at 11:24

1 Answers1

2

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.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • I still get the error Traceback (most recent call last): File "class1.py", line 45, in c = Pcs("PC1","Bl1",20,30,b) File "class1.py", line 34, in __init__ WS(obj,name,location,cpu,mem) File "class1.py", line 19, in __init__ self.dcname =obj.name #To which data center it is associated AttributeError: 'int' object has no attribute 'name' – Rajeev Dec 19 '10 at 11:32
  • 1
    @Rajeev: You do not "still get the error" that's a completely different error, and a different question. Also, in your code above, you clearly create a WS instance with no errors in this code `b = WS("WS1","Bl1",21,31,a) `, so your claim that you get an error is incorrect. – Lennart Regebro Dec 19 '10 at 11:36
  • Can u please let me know the complete code, if u can edit in your answer it would be great – Rajeev Dec 19 '10 at 11:38
  • 1
    Try looking at the code again. Notice the difference between the names of the arguments you pass, and the names of the arguments you receive? Specifically, notice the difference in their **order**? – Karl Knechtel Dec 19 '10 at 11:59
  • @Rajeev: I just *did* let you know the complete code. Are you even reading my answer? – Lennart Regebro Dec 19 '10 at 12:02