Some Noob Questions

A forum for general discussion of the Python programming language.

Some Noob Questions

Postby atxpython » Fri Sep 16, 2016 1:26 am

Alright...
So I've been playing around with Python for a few weeks now, and I have a few questions -

A) Importing Modules/Packages/Libraries
A.1) What is the preferred name for libraries that get imported into your script. I've seen them called modules, packages, libraries..
A.2) When is the best time to import said items?
I've always done all of my importing at the top of the script, however I'm starting to see examples where people import just before using the functionality they are importing.
Is there any benefit in doing it either way?
A.3) from x import y vs. import x. Is there any performance difference?

B) Functions vs. No Functions
I have a script that processes some data from one file and writes it to another - once its completed, the script closes.
A buddy saw it and critiqued that it didn't use any functions, rather it simply runs through the actions from top to bottom.
My questions is this:
Why does having functions matter, if I'm never reusing anything in the code. The code essentially does a series of unique things 1 time, then closes.
I can understand using functions if I were to use the exact same tasks over and over again, however I'm not.. So what would be the benefit here? Slightly more readability?
atxpython
 
Posts: 12
Joined: Fri Sep 09, 2016 9:49 pm

Re: Some Noob Questions

Postby metulburr » Fri Sep 16, 2016 1:34 am

A.1) What is the preferred name for libraries that get imported into your script. I've seen them called modules, packages, libraries..

A package is a collection of modules, while a module is a single python file. More info here
Modules part 1

I've always done all of my importing at the top of the script, however I'm starting to see examples where people import just before using the functionality they are importing.

99% of the time your imports should always be at the top, if not they should be relatively close. Sometimes certain modules will only be imported based on a previous imported module or what operating system. This can end up making import lines much lower.

Is there any benefit in doing it either way?

readability/maintainability
Everyone looks for imports at the top.

A.3) from x import y vs. import x. Is there any performance difference?

No this is all just how you import a module. See the link i provided before

Why does having functions matter, if I'm never reusing anything in the code. The code essentially does a series of unique things 1 time, then closes.
I can understand using functions if I were to use the exact same tasks over and over again, however I'm not.. So what would be the benefit here? Slightly more readability?

A good rule of thumb is if you copy and paste more than twice, you need to make a function out it. After that you could just be making more work for yourself later, as well as making your code spaghetti. To tell exactly we would have to see the code to determine if its too redundant.
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Some Noob Questions

Postby wavic » Fri Sep 16, 2016 5:48 am

If there is repeating blocks of code in your script you put it in a function, so you can save typing, your time, preventing misstypes, use the function as a parameter to other functions if returns some value/s.
Usualy if the script is small all code is inside main() function which is called like that.

Code: Select all
#!/usr/bin/env python3

import sys

def my_awesome_function():
    pass

def main():
    my_awesome_function()
 
if __name__ == '__main__':
   sys.exit(main())

In that way you can use the function in another script and do something else. Using the first one as a module. Without the .py extension, just the name of the file

Code: Select all
from my_first_script import my_awesome_function

This is not a law. Is not bad code if don't use a function. It's depending what you are doing.
wavic
 
Posts: 165
Joined: Wed May 25, 2016 8:51 pm

Re: Some Noob Questions

Postby ackmondual » Tue Sep 20, 2016 11:37 pm

atxpython wrote:Alright...
So I've been playing around with Python for a few weeks now, and I have a few questions -

Welcome to Python programming! And this forum! 8-)

atxpython wrote:A) Importing Modules/Packages/Libraries
A.2) When is the best time to import said items?
I've always done all of my importing at the top of the script, however I'm starting to see examples where people import just before using the functionality they are importing.
I have never seen this before (anywhere but the top), but TBF, I've looked at relatively little Python code, and the glut of code reading I did was last decade with other languages.

atxpython wrote:Is there any benefit in doing it either way?
Perhaps not, but I'd rather play it safe by putting them at the top.
I agree with the other post with keeping them at top, grouped together... I wouldn't want to have to hunt down imports or "Ctrl+F" them.

I suppose putting the imports with the functionality ties it to just that, but my "workaround" with putting them all at the top would be to just put in comments to indicate that. For example, random is used for dice rolling. re is used for the areas where it parses the url inputs

atxpython wrote:B) Functions vs. No Functions
I have a script that processes some data from one file and writes it to another - once its completed, the script closes.
A buddy saw it and critiqued that it didn't use any functions, rather it simply runs through the actions from top to bottom.
My questions is this:

Why does having functions matter, if I'm never reusing anything in the code. The code essentially does a series of unique things 1 time, then closes.
I can understand using functions if I were to use the exact same tasks over and over again, however I'm not.. So what would be the benefit here? Slightly more readability?

In the whole scheme of things, it probably doesn't matter that you didn't use functions to make things modular. On the philosophical side of things I can think up of...

1) it does make code more readable. I would've said the same thing... if you copy and paste blocks of code, then "you're doing it wrong". Anybody, including yourself in the near future will look at a function called "parse_THAT_input_from_whatever()" and know what's supposed to be going on when you look at that block of code.

Not having to paste 20 to 400 lines of code at once adds to the readability.

Also makes it more maintainable. If the function changes one day, you'd need to go through each instance where you pasted that block of code and ensure all changes are done, and correctly.

2) it's a good habit to get into
I too admit I avoid functions for shorter/quickie scripts, and sometimes they're genuinely not called for, but it's generally good not to breed habits otherwise.

3) interviewers or those in the know will be looking for this. Demonstrates knowledge and that your code will be scalable.

On a final note... I had a comp sci professor who was just plain shocked when he noticed that for one JAVA project, some students were submitting code that was just one, large, main function of 1500 or so lines. There were not requirements that the code be modular with functions (even though the guidelines mentioned it should be). However, we had 2 more projects that expanded on that first one, so using functions and the modular stuff would've gone a long way towards those next 2 projects.
ackmondual
 
Posts: 44
Joined: Tue Apr 07, 2015 6:33 am

Re: Some Noob Questions

Postby atxpython » Wed Sep 28, 2016 10:07 pm

metulburr, wavic, and ackmondual:

Thank you all for taking the time to reply to my post!

The more I play around with Python, the more I can see little projects where it will be incredibly useful.
Making my code scalable would be huge in terms of future use.

I guess I just wasn't thinking that I'd ever use this code for anything else...
And guess what... I've already found a larger programming project to work on, where I could definitely use the features provided in my small program.
So, here I am... Re-writing the first program in a more modular way, so I can use it later.

Ha - lesson learned.
atxpython
 
Posts: 12
Joined: Fri Sep 09, 2016 9:49 pm


Return to General Discussions

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron