Procedures, Python Programming

 

In Python, the fundamental abstraction of a computation is as a procedure (other  books call them "functions" instead; we will end  up  using  both  values).   A function that  takes  a number as an argument and returns the argument value  plus 1 is de?ned as:

 

def f(x):

return x + 1

The indentation is important here, too. All of the instructions of the procedure have to be indented one level below  the def.  It is important  to remind the return statement at the end,  if you want your  method to give a value.  So, if you described f as above,  then  operated with  it in the shell,4 you may  get something like that:

 

>>> f

>>> f(4)

5

>>> f(f(f(4)))

7

 

If we just evaluate f, Python tells us it is a method. Then we can give it to 4 and get 5, or give it multiple times, as given.

What if we de?ne

 

def g(x):

x + 1

Now,  when  we play with it, we might  get something like this:

>>> g(4)

>>> g(g(4))

Traceback (most recent call last):

File "", line 1, in ? File "", line 2, in g

 

TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

 

What happened!! First, when  we calculated g(4), we take nothing at all, because  our definition of g did not give anything. Well...strictly speaking, it given a special number known as None, which the file does  not bother printing out.  The number  None has a special  type,  known  NoneType.   So, then,  when  we tried  to give g to the  answer  of g(4),  it ended up  trying  to calculate g(None), which  made  it try to evaluate None + 1, which  made  it complain that  it did  not know  how  to add  something of type NoneType and something of type int.

 

Posted Date: 8/9/2012 7:00:30 AM | Location : United States







Related Discussions:- Procedures, Assignment Help, Ask Question on Procedures, Get Answer, Expert's Help, Procedures Discussions

Write discussion on Procedures
Your posts are moderated
Related Questions
Procedures in Firrst-class objects In Python, unlike  many  other  languages, methods are behave in much  the same way as num­ bers:  they  can be stored as values  of variabl

Non-local references So far, whenever we needed to compute a variable, there  was  a binding for that  variable in the 'local' environment (the environment in which  we were e

Programs and Data Object-oriented programming is a popular way  of managing programs, which  groups together data  with  the procedures that  works on them,  thus  facilitatin

Procedures as First-class objects In Python, unlike  many  other  languages, methods are behave in much  the same way as num­ bers:  they  can be stored as values  of variable

I need server, client and trusted side communicating and charging a service with that protocol (or needham shroeder)

Tower of Hanoi game that you can let a player to move discs between the towers using a mouse. Moreover, you are required to do the followings: •Graphically represent any state in t

Procedures as first-class objects In Python, unlike  many  other  languages, methods are behave in much  the same way as num­ bers:  they  can be stored as values  of variable

Basics of python-introduction Python is designed for easy interaction between a user and the system. It goes with  an inter­ active function  known as  a shell or listener.  T

#question.Program 10 Assignment (Banking Objects – Savings/Checking Accounts) Create a program named 10.py that performs the following: Create a class named ChkAcct, and a class n

Range Very frequently, we will want to iterate through a list of integer values, often as indices.  Python gives a useful function, range, which gives lists of integers. It ma