Python Tutorials

11. How to find the current module name in python

By: Python Documentation Team : 2012-04-07

Description: module can find out its own module name by looking at the predefined global variable __name__. If this has the value '__main__', the program is running as a script. Many modules that are usually used by importing them also provide a command-line interface or a self-test, and only execute this code after checking __name__:


12. How to force rereading of a changed module in python

By: Python Documentation Team : 2012-04-07

Description: For reasons of efficiency as well as consistency, Python only reads the module file on the first time a module is imported. If it didn’t, in a program consisting of many modules where each one imports the same basic module, the basic module would be parsed and re-parsed many times. To force rereading of a changed module, do this


13. UnboundLocalError in python

By: Python Documentation Team : 2012-04-07

Description: It can be a surprise to get the UnboundLocalError in previously working code when it is modified by adding an assignment statement somewhere in the body of a function.


14. call by reference in python

By: Python Documentation Team : 2012-04-07

Description: Remember that arguments are passed by assignment in Python. Since assignment just creates references to objects, there’s no alias between an argument name in the caller and callee, and so no call-by-reference per se. You can achieve the desired effect in a number of ways.


15. Callable objects in python

By: Python Documentation Team : 2012-04-07

Description: You can make a higher order function in Python using two choices: you can use nested scopes or you can use callable objects. For example, suppose you wanted to define linear(a,b) which returns a function f(x) that computes the value a*x+b. Using nested scopes:


16. Ternary operator in python

By: Python Documentation Team : 2012-04-07

Description: C language's ternary operator equivalent was added in Python 2.5. The syntax would be as follows


17. Hexadecimal and Octal integers in python

By: Python Documentation Team : 2012-04-07

Description: To specify an octal digit, precede the octal value with a zero, and then a lower or uppercase 'o'. For example, to set the variable 'a' to the octal value '10' (8 in decimal), type:


18. Convert string to number in python

By: Python Documentation Team : 2012-04-07

Description: For integers, use the built-in int() type constructor, e.g. int('144') == 144. Similarly, float() converts to floating-point, e.g. float('144') == 144.0.


19. Convert number to string in python

By: Python Documentation Team : 2012-04-07

Description: To convert, e.g., the number 144 to the string ‘144’, use the built-in type constructor str(). If you want a hexadecimal or octal representation, use the built-in functions hex() or oct(). For fancy formatting, see the String Formatting section,


20. Perl's chomp() equivalent for removing trailing newlines from strings in python

By: Python Documentation Team : 2012-04-07

Description: Starting with Python 2.2, you can use S.rstrip("\r\n") to remove all occurrences of any line terminator from the end of the string S without removing other trailing whitespace. If the string S represents more than one line, with several empty lines at the end, the line terminators for all the blank lines will be removed