Click here to Skip to main content
15,886,104 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
class student :
  def __init__(self,name,rno) :
    self.name=name
    self.rno=rno
    self.Lap=self.Laptop 
  def show(self) :
    print(self.name,self.rno)
    self.Lap.show()
  class Laptop:      #inner class
    def __init__(self) :
      self.brand="hp"
      self.cpu="i5"
      self.ram=8
    def show(self):
      print(self.brand,self.cpu,self.ram) 
s1=student("ravi",2)
s2=student("kumar",3)
s1.show()

I am getting an error please help me

What I have tried:

I am tried but getting an error

ravi 2


Warning\Error

Traceback (most recent call last):
  File "file.py", line 18, in <module>
    s1.show()
  File "file.py", line 8, in show
    self.Lap.show() #inner print
TypeError: show() missing 1 required positional argument: 'self'
Posted
Updated 31-Jan-20 23:20pm
v2

You forgot the parentheses in your call to create an instance of the class Laptop. It should be:
Python
class student :
  def __init__(self,name,rno) :
    self.name=name
    self.rno=rno
    self.Lap=self.Laptop() # <- note trailing parentheses
 
Share this answer
 
You don't need to pass self to a function: it's there in all classes automatically - it's the current instance of the class that contains the function.

Think of it like cars: all cars have a glove box, so you could write a function to add or remove an item from it - but you don't need to pass the car to the function because the glove box is "attached" to a specific cars already! You say "put my mobile in the glove box of my car" and "my car" is the instance: it already knows which glove box you are talking about because it automatically gets the car massed as self

Just remove the self from the function definition.
 
Share this answer
 
Comments
Richard MacCutchan 1-Feb-20 7:02am    
The keyword self is required, and is the equivalent of this in C++/C# etc. Bizarrely you can use any word to represent this parameter, and indeed, different words within the same class definition. You have to laugh.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900