0

I am learning python from "Learn Python The Hard Way", I came across a source code

"from sys import argv" and got an explanation that sys is a module from where argv is imported.

Although, sounds simple but still not able to understand the concept behind. I would like to know how they picked up the module sys or how we gonna decide from where we need to import.

please help!

5
Contributors
4
Replies
33
Views
6 Months
Discussion Span
Last Post by hastings.george.5
2

I'm really sorry, I don't know Python. But I just felt compelled to ask ... why choose to learn Python the hard way? What's wrong with the easy way?

3

The import statement is the mechanism that allows python programs to use libraries. Using libraries is a key feature in any programming language. It allows our programs to benefit from all the work that has been done before by others.

Suppose you're a clever mathematician and you write a python function to factorize integers, then you will write your own module named for example clever_mod and this module will contain a function factorize(). If you publish your module on pypi for example, anybody can download it an use your function by writing

from clever_mod import factorize
result = factorize(9237982379866234)

As of today, there are 97775 python modules in pypi, and an unknown number of available modules that are not on pypi.

sys is a standard library module in python, which means that it is one of the few dozens modules that automatically come with every python install. sys.argv is the list that contains the arguments that your script received from the command line when its execution was launched.

@Dani "Learn python the hard way" is a well known python tutorial. The idea is to get your hands dirty by actually typing every character of the tutorial's programs and examples in order to learn python faster. It sounds like a reasonable idea.

Edited by Gribouillis

1

Beside Gribouillis' explanation.

Although, sounds simple but still not able to understand the concept behind.

Core concepts are the most hard things.

I would like to know how they picked up the module sys or how we gonna decide from where we need to import.

The author picked it, because the desired object - the argv variable - is in this module. Period, end of discussion.

Why this functionality is in this module is because the author of python interpreter - beeing a built in module - decided to put it into this module.

How an author of a module decides about a module name, and the functionality of the module is up to his judgement.

However, module names are considered good, if a user (a programmer) can safely guess the functionality of a module by its name, and can find a module name by knowing the functionality he expects.

Based on my exprience on programming I would look for "command line argument", if I did not know python:

  • In builtins or globals, as in C or perl
  • in some operationg system specific module, because its os specific
  • in some system specific module, because it is global to a program
  • in something called shell, because shell is the first to process it

Anyway a lasy programmer usually types into the google: Command line arguments python
And gets the answer...

Edited by slate

0

Just to answer in simple terms, you are right saying sys is a module. How come then you know that? Thats what you've asked, I think. Let me say this: From every module we import, there are methods. In each module there are functions, that operate on data that is passed as arguments. Period! Now comes the need for you to read thoroughly the documentation for the particular language at hand, in your case python.

argv is used to pass values, for instance, argv[0] is the first value passed, and so on.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.