[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Need Python help!

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 17
Thread images: 5

File: 1452755022140.png (696KB, 633x758px) Image search: [Google]
1452755022140.png
696KB, 633x758px
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
>>
File: 1453682586050.png (9KB, 338x339px) Image search: [Google]
1453682586050.png
9KB, 338x339px
>>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
>>
File: 1452228810512.png (53KB, 300x300px) Image search: [Google]
1452228810512.png
53KB, 300x300px
>>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?
>>
File: 1452281544451.jpg (176KB, 1632x1224px) Image search: [Google]
1452281544451.jpg
176KB, 1632x1224px
>>58133908
This is how they were in my code before
class 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 like
var 

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.
>>
File: 1452915192820.png (14KB, 635x773px) Image search: [Google]
1452915192820.png
14KB, 635x773px
>>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 look
class 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()

>>
>>58133724
class 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.
Thread posts: 17
Thread images: 5


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.