+ 3

def__

class wow: def__init__(s,x): s.x = x def__add__(s,y): return wow(s.x - y.x) def__sub__(s,y): return wow(s.x + y.x) def__repr__(s): return str(s.x) a = wow(5) b = wow(7) c = wow(3) print(a-b+c) This is one of quiz from python challenge. When I ran it on idle it show syntax error at line 2 for def__init__(s,x): for this semicolon thing. why so?

18th Dec 2017, 2:27 PM
Kanhaiya Kr
Kanhaiya Kr - avatar
4 Antworten
+ 2
Write an space between def and __init__ :) (also with the other defs) To @Vukan , the "self" word is just a convention, you can use any argument, but this must be also the same in all the class. (But the best option is always to use "self" to refer the self object :) )
18th Dec 2017, 2:33 PM
Sebastián Zapata
Sebastián Zapata - avatar
+ 6
you must use self for init
18th Dec 2017, 2:28 PM
Vukan
Vukan - avatar
+ 1
How so?
18th Dec 2017, 2:30 PM
Kanhaiya Kr
Kanhaiya Kr - avatar
+ 1
Thanks Sebastian. It worked
18th Dec 2017, 2:37 PM
Kanhaiya Kr
Kanhaiya Kr - avatar