Python: constructors | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python: constructors

can anyone explain the constructor in python how it differs from java.

28th Sep 2018, 6:40 AM
Umesh Tikhe
Umesh Tikhe - avatar
4 Answers
+ 3
self - is used to reference current object (instead of keywords like in some other languages). It's just a variable, so you can change its name, but self - is a standard one
28th Sep 2018, 9:17 AM
WittyBit
WittyBit - avatar
+ 4
In Python you can have 1 constructor (function with name __init__) that takes self reference and arguments: def __init__(self, ...) You create fields just by assigning a value to them in the constructor: def __init__(self): self.x = 5 In Java fields are part of class structure
28th Sep 2018, 9:00 AM
WittyBit
WittyBit - avatar
0
why we every time write self keyword when creating constructor or any function
28th Sep 2018, 9:03 AM
Umesh Tikhe
Umesh Tikhe - avatar
0
ok
28th Sep 2018, 9:18 AM
Umesh Tikhe
Umesh Tikhe - avatar