0
Where did I go wrong?
https://code.sololearn.com/c2ZapmYH9P2g/?ref=app Can you kindly explain magic methods with different examples.. I mean with one argument, two arguments.. in different formats
28 ответов
+ 5
Replace self.o by o.x .
As student class doesn't have an attribute named o .
+ 4
inside __add__
return Student(...) means you are returning an object of student class.
+ 4
Look at this. Maybe it's more clear?
class Student:
def __init__(self, x):
self.x = x
#self.y = y
def __add__(self, other):
return Student(self.x + other.x)
t = Student(2)
g = Student(5)
e = t + g
print(e.x)
https://code.sololearn.com/cvNjntUJ7M0T/?ref=app
https://code.sololearn.com/cRVc0Lvpa1qK/?ref=app
+ 3
X is an attribute of student class.
+ 3
Ok,
q + w
invokes the magic method
q.__add__(w).
There can only be two objects that get added together at once, so you can't have more than two parameters to that magic method (self and other). Remove "other1" from the __add__() method.
(q+w).x causes an error because q+w returns an int and an int does not have the attribute x. As you have been told by Coding Cat [Mouse searching] already, you need to modify __add__() so that it returns a Student object, i.e. return Student(self.x + other.x). That way, q+w will return a Student object which will have the attribute x.
This very simple amendment means that the following code snippet works fine.
class Student:
def __init__(self, x):
self.x = x
def __add__(self, other):
return Student(self.x + other.x)
q = Student(5)
w = Student(5)
z = Student(5)
e = q + w + z
print(e.x) # 15
print((q+w+z).x) # 15
+ 3
P.M. in my mm_test2, e is N O T a Student, he is a Student2
type(e) = Student2
e supports addition, u cant add Student objects but Student2 objects take properties from Student and then add __add__ method that makes the Student2 support additive operation with '+' operator
+ 2
No, not quite. An object is an example of a class. You can have multiple objects of the same class. But an object can only be of one class type (excluding inheritance/extensions). e = Class() will not work as you haven't specified a class that the object will be from.
An attribute is a quality or measure of an object instance.
Think of classes as blueprints for something. They describe how something built to the specifications will look or behave etc. An object will be something that is built following those blueprints (be it a house or a car). Finally, an attribute is something that describes a particular object in some way.
Don't feel bad if you are finding it hard to understand this. This is one of the hardest parts of basic Python, and can require a lot of work and practice to understand.
+ 2
NeoScorpioN i was not talking about your code, i was talking about russ' answer.
+ 1
😂 Sir Isaac Newton !! When will you leave this world bro?? You are killing our souls using physics.. and you are here again in programming field.. Please don't invent something new here.. Ok whatever..
Q) Inside __add__() method I wrote Student(........) What should I call this?? Object of student class?? Constructor?? And why I need this to use magic methods?Can't I do any operation without it while using magic methods?
+ 1
As someone already pointed, the __add__ method only accepts two arguments. Remove the 'other1'. You can then add them in pairs.
Also, notice that you are taking the x property/attribute from these objects, adding them up and returning the result as an integer. This means you are not creating a new Student instance, and thus using the dot operator as e.x means nothing to the interpreter.
+ 1
Mohammad Mehedi Hasan Yes, you have to specify what type of object will be returned. You can't trust a computer to "work out" what it is you want and make a common-sense-based decision.
To answer your other question; now that you have two parameters to your Student class (x and y), whenever you create an object of that class, you must pass two parameters to it. I amended your code like this. Let me know if this is what you were after.
class Student:
def __init__(self, x, y):
self.x = x
self.y = y
class Student2(Student):
def __add__(self, other):
return Student2(self.x + other.x, self.y + other.y)
q = Student2(5,5)
w = Student2(5,5)
z = Student2(5,5)
e = (q + w + z)
print(e.x) # 15
You said that you only want __add__() to add the x attribute and this one adds both x and y. You just need to decide what value you want the y attribute to be in your new Student object. If you wanted 0, you can change self.y + other.y to 0.
+ 1
Mohammad Mehedi Hasan By "type of object" I mean the class of which the object is. In your example code, you have created objects called q, w, z and e. These are all objects and they are all of the Student class. ints and lists are also classes and if you have an int (say x = 1), then you can consider x an object of the int class.
Whether you want to return an int, list, or a Student, you must specify what type it is you are after. So return Student(...) returns an object of the Student class. This can be seen by the fact that you have assigned this object to the name e. e has attributes x and y which you can access as you wish.
Hope that clears it up.
+ 1
Russ Ok, suppose #Samsung is a class
#Laptop, Android are objects
#Details about them are attributes
#And running, showing, playing music, OS are methods
Am I right??
And what is this e..
e = Classname()
I learned that it's an object of that class and that's why we call it OOP😂
+ 1
Mohammad Mehedi Hasan Yes, I think you've got it!
e is just an example of a name you could give to an object. Maybe I misunderstood you earlier. If you have
e = Student()
then e is indeed an object of the class Student. Sorry if I confused you.
+ 1
e is an object of Student class in reality.
+ 1
Mohammad Mehedi Hasan u could use more subclasses
0
Sir Isaac Newton but x was an parameter.. um.. or attribute 🤔.. What is x??
0
Then how is it an object inside add method??
0
Sir Isaac Newton I think self.x is the attribute and x is the parameter..?