+ 1
How can i make a simple constructor in Python?
7 Antworten
+ 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.
+ 2
Haha ok. :) Well you weren't that clear on the details, he he ;-)
+ 2
I know, Sorry :P
+ 1
Then I need to do It inside a class, right? Or is there a way to do It outside?
+ 1
uhm..I realized that what I was trying to do could be solved by using while loops 😂 But anyway, thanks everbody!
0
define __init__ function.
0
Yes the constructor goes inside the class definition.