Hi All,

Im new to Python and doing a few basic exercises around modules and calling same. Can someone explain what par is in relation to this example?

--------------

# Defining a module called support.py

def print_func( par ):
   print "Hello : ", par
   return

--------------

# Importing support.py module 

import support
support.print_func("Zara")

Thanks

Tubs

To get more information on par you can do this:

# Importing support.py module 

import support
support.print_func("Zara")

# to get more insight ...
import inspect
print(inspect.getcallargs(support.print_func, "Zara"))

''' result ...
Hello :  Zara
{'par': 'Zara'}
'''

Edited 2 Years Ago by ZZucker

Comments
nice!

Par is parameter of function print_func residing in module support.

Thanks. Just to clarify:

  1. Is "Par" a pre-defined command in Python and if so is it always used when defining parameters for functions?
  2. Are there alternatives, ie: could "Par" be replaced with a different commmand or phrase?
  1. Par is not same as par in Python, they would be two variable names.
  2. Variable names are free, but you should really make very good, descriptive names, rules for names can be found in Python documentation, shortly lower and upper case letters, _ and numbers (except for first letter) are allowed. You should use small first letter for variable names and procedure names, underscore between words: my_variable. For class names Capital first letter for every word (MyOwnClass), space is not allowed.

Par is not same as par in Python, they would be two variable names.

Sorry I meant par

In your case def print_func( par ): argument par could have other more decriptive names, just make sure you don't use builtin Python function names.

Right, so "par" is not a pre-defined Python command/phrase, it is a user defined phrase to describe a specific parameter. In what way is the use of Parameters similar/different to Variables? Same thing?

Sorry about the noob questions.

In what way is the use of Parameters similar/different to Variables? Same thing?

The correct python concept is the namespace. At any given moment in the execution of python code, a certain number of names are defined in the current namespace. There is no difference between parameters and variables in this respect. For example in the following code

foo = 10

def bar(qux):
    baz = 23

the local namespace during the execution of function bar() contains the 2 names qux and baz, whereas the global namespace contains the names foo and bar, and a few other predefined names such as __name__ .

Edited 2 Years Ago by Gribouillis

Got it, so par in this case is a variable but in Python specifically its called a name?

par in this case is a variable but in Python specifically its called a name

Exactly. The namespaces are also called symbol tables. The functions locals() and globals() return the current symbol tables as dictionaries. Notice that locals() is a read-only dict.

Edited 2 Years Ago by Gribouillis

It is defined in local container of variables which we call namespace. Namespace can contain locally same variable names as global namespace, but it does not change value of global variable.

The functions locals() and globals() return the current symbol tables as dictionaries

This sounds useful. I had a tinker:

a=3
b=4
c=5
d=6
globals()

Returned

{'a': 3, 'c': 5, 'b': 4, 'help': , '__builtins__': , '__package__': None, 'sys': , '__name__': '__main__', '__doc__': None, 'd': 6}

What are all of these other objects ie: builtins etc?

If you want the names only (and not the values of the variables), you can use the dir() function. Names with double underscores on both sides are normally reserved for python's internal mechanics. Here you have the global names

  • __builtins__: the default module __builtin__ (without s) which contains the builtin functions.
  • __name__: the python name of the current module, which is the string '__main__' for the main script, but it would be 'xml.dom.minidom' if this code was in the module xml.dom.minidom.
  • __package__: the name of the package containing the current module if any. It would be 'xml.dom' for module xml.dom.minidom.
  • __doc__: the documentation string of the current module if any.
  • sys: module sys if you wrote import sys somewhere.

In imported modules, there is usually also __file__, the system path to the file where the module's code was loaded if any.

Edited 2 Years Ago by Gribouillis

This question has already been answered. Start a new discussion instead.