0
[Solved] Could you check this Python class out to tell me if initialize is used just to start the instance?
1) We could practically write anything in that string value for the attribute q in initialize. But we can have the same output without the initialize, so why do we even do it? 2) Do you consider this exercise good for beginners? Would this example be ever useful or was it just asked, in your opinion, to make us understand? Thank you. It's level 1 but looks more confusing to me than the class examples I've seen so far. https://code.sololearn.com/caB0BbFoEg6m/?ref=app
5 Respuestas
+ 5
It is the conceptual idea behind.
Each object has a state.
The state of an object is defined by its attributes.
At the initialisation the state of your object is not clearly defined because self.a is not defined.
n=Srg( )
#n.getString( )
n.printString()
would raise problems.
So you'd better initialize self.a
Initialisation of self.q is useless.
+ 1
Even if you don't write the __init__ it will still be called (from the superclass)
https://www.askpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK/oops/python-class-constructor-init-function
0
By doing it without initialize I mean:
class Srg:
#def __init__(self):
#self.q = "dfhjklol" #anything goes when #uncommented out, bcs,
def getString(self):
self.a = input('Enter input: ') #bcs, reassign
def printString(self):
print(self.a.upper())
n = Srg()
n.getString()
n.printString()
I mean what needs initialization when you can get rid of it?
0
Oma Falk Hi, thank you. Edit: You mean the q that I commented out. OK. Thanks for the last bit, it's really useful.
0
Although, Lisa says it's gonna initialize anyway, so why would the implicitly initialized one pose problems?
I'll set out to read the link Lisa's provided. Thanks, Lisa, btw.