+ 1

How can i make a simple constructor in Python?

21st Dec 2016, 9:16 PM
IgorSM
IgorSM - avatar
7 Answers
+ 4
That is a great request, thanks for letting me explain it for you. Let's make an example; class Test(object): def __init__(self, x): self.x = x t = Test(10) print t.x z = Test(20) print t.x if you want custom values for the x-attribute all you have to do is to pass that value as argument to __init__: Hope this helped. :) Dr.
21st Dec 2016, 9:36 PM
Tristan McCullen
Tristan McCullen - avatar
+ 2
Haha ok. :) Well you weren't that clear on the details, he he ;-)
21st Dec 2016, 10:09 PM
Tristan McCullen
Tristan McCullen - avatar
+ 2
I know, Sorry :P
21st Dec 2016, 10:09 PM
IgorSM
IgorSM - avatar
+ 1
Then I need to do It inside a class, right? Or is there a way to do It outside?
21st Dec 2016, 9:34 PM
IgorSM
IgorSM - avatar
+ 1
uhm..I realized that what I was trying to do could be solved by using while loops 😂 But anyway, thanks everbody!
21st Dec 2016, 10:07 PM
IgorSM
IgorSM - avatar
0
define __init__ function.
21st Dec 2016, 9:32 PM
Rishi Anand
Rishi Anand - avatar
0
Yes the constructor goes inside the class definition.
21st Dec 2016, 9:35 PM
Rishi Anand
Rishi Anand - avatar