0
Why it is showing name error and please name some YouTube channels for learning python
5 Answers
+ 6
class nikhil():
def __init__(self,ram,cpu):
self.ram=ram
self.cpu=cpu
def conf(self,ram,cpu):
self.ram=ram
self.cpu=cpu
print("configuration is",self.cpu,self.ram)
com = nikhil(8,"ryzen")
com.conf(16,"whatever")
__init__() needs double underscores, not single
When creating an instance of the class, you need to call the class nikhil, not the class method conf
Don't pass "self" as a parameter when you call the class method
+ 3
https://code.sololearn.com/cTFosXhrKm9B/?ref=app
Ask if something u dont understand
+ 3
class nikhil:
def __init__(self,ram,cpu):
self.ram=ram
self.cpu=cpu
def conf(self):
print("configuration is",self.cpu,self.ram)
com = nikhil(8,"ryzen")
com.conf()
Edit: ShortCode Ay, the exact same changes! :>
+ 2
You're not giving arguments to the class, but to the class instance. That's what __init__ is for. com = nikhil(8, 'ryzen') creates a new instance of the class "nikhil", calls __init__ and passes the parameters 8 and 'ryzen' to self.ram and self.cpu respectively.
com = conf(8, 'ryzen') tries to access a class method without creating an instance of the class. Within the method, you use self.cpu and self.ram but there is no "self" you can refer to. "self" always refers to an instance of the class.
0
Okay, I am bit confused. Why we are giving arguments in class nikhil, If arguments in method are what we get. And if I changed the arguments in class nikhil, it is showing error. So why it is. I am new to oop, I started it today with class.