Skip to main content
11 events
when toggle format what by license comment
Jan 3, 2015 at 20:45 comment added jonrsharpe There isn't a general case. Sometimes class and instance attributes are appropriate, sometimes you need explicit parameters and return values. In this case, as you don't need the class, you should use the latter. global scope is rarely the right choice, except for constants.
Jan 3, 2015 at 20:05 comment added nrp One last moment: how would you recommend to declare variables in a general case? Is is acceptable to make them global like I did or to use smth similar to getter (it's properties in Python, right?) and to make variables private?
Jan 3, 2015 at 19:53 vote accept nrp
Jan 3, 2015 at 19:50 comment added jonrsharpe Why have you done the import like that?! Standard library modules will always be available (and your script won't work without them), import random as rnd is perfectly acceptable, and will throw an appropriate ImportError if something does go wrong. And you can't import from strings like that; see e.g. stackoverflow.com/q/301134/3001761
Jan 3, 2015 at 19:49 comment added jonrsharpe Putting the whole script inside a main is one way to do it, but it would be much better to split into lots of short functions, then have a main that just calls the appropriate functions at the appropriate times.
Jan 3, 2015 at 19:49 comment added nrp And the second is that I've tried to put all imports inside try-except blocks that I have also in the beginning to one function but did not succeed. Can you please comment? def import_module(module_name, custom_module_name, message): try: import module_name as custom_module_name except ImportError: print(message.format(module_name)) import_module("random", "rnd", "Importing module '{}' did not succeed")
Jan 3, 2015 at 19:46 comment added nrp First of all, thanks again for your remarks. I used the dict and updated the main question, pls take a look. And I also would appreciate if you'll comment on 2 more things. First is an entry point that you've mentioned (sorry, I didn't quite got it - did you mean to put all the main body of a script into a main method?
Jan 1, 2015 at 14:38 history edited jonrsharpe CC BY-SA 3.0
added 171 characters in body
Jan 1, 2015 at 14:26 comment added jonrsharpe @nonrandom_passer but those methods are all duplicates. If you adopt the dictionary version, all you need is one function to parse a specified file to a config dictionary, then dict.update in the correct order. You could also use an existing file format, e.g. JSON, rather than define your own.
Jan 1, 2015 at 14:23 comment added nrp Thanks a lot for answering. About code at the top level - I'm currently writing functions, and what you've written is very handy for me now. I'll probably leave the class because it is comfortable for me to have structure like that when I can just add more methods inside if there would be, say, more config files.
Jan 1, 2015 at 13:55 history answered jonrsharpe CC BY-SA 3.0