Hey guys Im new to python and I need help. Is it bad practice to initialize variables to None inside the __init__ method of a class?
Here is my code:class Loader():
def __init__(self):
self.client_secret = None
self.client_id = None
self.username = None
self.redirect_uri = None
self.token = None
Its impossible for me to initialize them before hand because it requires user input. Should I just call the input() method inside the __init__ ? Or is there a better way to do this
>>58133724
1. Get user input
2. Create object using data from step 1
Put it all in a factory function if you feel like it
Thank you for formatting your code unlike most people who ask.
>>58133724
It's not very pythonic
>>58133793
Im a little confused, you want met to ask for input before creating my Class object? Is there no way to do it from inside the class? Thank you anyways anon.
>>58133798
np
>>58133806
How would I make it pythonic then? My code before was just all functions before, but I changed it to OO design because I have like 8 global variables. I figured it would be better to have all the variables inside a class rather than the whole file. Is that bad?
>>58133830
I have no idea how to make it more pythonic, (you should ask on #python on irc.freenode ) It's likely smart to put it in a class
>>58133830
Don't bother. What matters is whether the code works, not whether you can post it on StackOverflow without triggering waves of "not pythonic" comments.
>>58133830
Nah, its fine m8. But why dont you just create the variables and initialize them when you read them in from the user?
>>58133908
This is how they were in my code beforeclass Loader():
def __init__(self):
self.client_secret
self.client_id
self.username
self.redirect_uri
self.token
But python kept giving me "Python AttributeError: Object has no attribute" When I would ask for user input to initialize them.
But the error went away when i initialized them to None and then later ask for input. But this doesnt feel right to set them all as None.
>>58133965
>But this doesnt feel right to set them all as None.
Why not? Some programming languages do that by default when declaring attributes/instance variables. Java sets them to 0, false, or null (equivalent of python None) depending on the type.
>>58133989
Oh wow, I guess I read the documentation wrong then. I could have sworn you could leave varaibles unitialized in python likevar
But I guess I read it wrong. After googling some answers people are either setting them None or to their appropriate type such as "" or 0. Thanks man!
Afaik it is not good design, because if you pass a None object to a function, it is likely to throw an error message.
But I get what you are trying to do - declare variables without initializing their values. That easy in languages where declaration and initialization are seperated. In Python ... no idea.
>>58133989
>>58134088
>>58133908
>>58133793
I have decided that this might be the best approach, without having to set up None variables. If you guys could take one last lookclass SpotLoader():
def __init__(self,client_secret,client_id,username,redirect_uri):
self.client_secret = client_secret
self.client_id = client_id
self.username = username
self.redirect_uri = redirect_uri
def setup_spotify_credentials(self):
self.client_secret = input("Enter Spotify Client Secret: ")
self.client_id = input("Enter Spotify Client Id: ")
self.username = input("Enter Spotify Username: ")
self.redirect_uri = input("Enter host IP: ")
#then something like this to initialize
def main():
spot_loader = SpotLoader()
spot_loader.setup_spotify_credentials()
>>58133724class Loader():
def __init__(self):
self.username = input('enter name')
# etc
# create object, initialize
details = Loader()
# print field
print(details.username)
>>58134190
why not have the input in the constructor?
>>58134440
>>58134190
These are both not good designs. Your input should be away from your classes. When you construct a class, you try to make that class work in different implementations (be it GUI or CLI).
setup_spotify_credentials should be removed, in the constructor you assume you have received those credentials (either be it from input, command line args, or gui). Before creating those attributes from the params, you should do validity checking and you are good to go.