0

Why it is showing name error and please name some YouTube channels for learning python

https://code.sololearn.com/cRttM3YWtD8e/?ref=app

7th May 2019, 5:32 AM
Nikhil Tale
Nikhil Tale - avatar
5 ответов
+ 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
7th May 2019, 5:43 AM
Anna
Anna - avatar
+ 3
https://code.sololearn.com/cTFosXhrKm9B/?ref=app Ask if something u dont understand
7th May 2019, 5:40 AM
ShortCode
+ 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! :>
7th May 2019, 5:42 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
7th May 2019, 8:37 AM
Anna
Anna - avatar
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.
7th May 2019, 6:35 AM
Nikhil Tale
Nikhil Tale - avatar