0

The below code can be found in product.py in product module in OpenERP 6.1

    _columns = {
        'qty_available': fields.function(_product_qty_available, type='float', string='Quantity On Hand'),
        'virtual_available': fields.function(_product_virtual_available, type='float', string='Quantity Available'),
...


    def _get_product_available_func(states, what):
        def _product_available(self, cr, uid, ids, name, arg, context=None):
            return {}.fromkeys(ids, 0.0)
        return _product_available

   _product_qty_available = _get_product_available_func(('done',), ('in', 'out'))
   _product_virtual_available = _get_product_available_func(('confirmed','waiting','assigned','done'), ('in', 'out'))

Could someone explain to me the purpose of defining method inside method in python in general and in this specific case ?

Jibin
  • 3,054
  • 7
  • 36
  • 51
  • In this specific example, it's not clear why there is a function in a function. But a common reason is *factory functions*. Take a look at the example in this question to see what I mean: http://stackoverflow.com/questions/901892/python-factory-functions-compared-to-class – SethMMorton Oct 09 '13 at 05:27

5 Answers5

3

Personally I use nested functions if they are kind of a helper function that is only used locally inside the outer method. Of course you can move methods to the module level and make them more "visible". For me they provide the chance to encapsulate code a bit better in some situations where the nested function is not of interest to code specified on the module level.

  • 3
    Also, closures. You can't get a closure using a non-local function. – Amadan Oct 09 '13 at 04:22
  • @user2799617 why use a function? are you are calling the nested function multiple times inside the main function? Doesn't nested functions make the main function less readable/cumbursome? Isn't a function supposed to do one task & isn't there like a advisable size limit (no of lines) in functions.Could you give some examples in PEP8 standards ? – Jibin Oct 09 '13 at 04:46
1

One potential benefit of using inner methods is that it allows you to use outer method local variables without passing them as arguments.

def helper(feature, resultBuffer):
  resultBuffer.print(feature)
  resultBuffer.printLine()
  resultBuffer.flush()

def save(item, resultBuffer):

  helper(item.description, resultBuffer)
  helper(item.size, resultBuffer)
  helper(item.type, resultBuffer)

can be written as follows, which arguably reads better

def save(item, resultBuffer):

  def helper(feature):
    resultBuffer.print(feature)
    resultBuffer.printLine()
    resultBuffer.flush()

  helper(item.description)
  helper(item.size)
  helper(item.type)

it is most often useful when the inner function is being returned (moving it to the outer scope) or when it is being passed into another function.

A nested function has access to the environment in which it was defined.

And Specific about product.py code it's about developer choice to manage the code to keep short readable function and defining more function which can be used as API.

Thank You

ifixthat
  • 6,137
  • 5
  • 25
  • 42
  • Could you take sometime and explain that these lines in the product.py code in detail.. – Jibin Oct 09 '13 at 05:27
0

In the given example, I could not tell you. In general you declare a function within a function for two reasons: to return a new function using the original functions parameters as context or to quickly encapsulate functionality repeated only within the original function.

Example 1:

def partial_add(value):
    def value_plus(y):
        return value + y
    return value_plus

f = partial_add(5)
f(6)
> 11
f(f(5))
> 15

Example 2:

def print_weird(p1, p2, p3, item, constant, k):
    def related(v, u):
        return v.score(u, item) < constant*(k+1)/k
    if related(p1, p2) and related(p2, p3) and related(p3, p1):
        print "weird"
brent.payne
  • 5,189
  • 3
  • 22
  • 17
0

I hope the use of nested functionality and its uses has been explained in other answers. So Im not going to it. In openerp, if you only check the product.py file in product module it will be bit confusing that what's the use of nested functionality. It is actually used in stock module. if you go through the product.py file in stock, you will be able to find the use of it.

OmaL
  • 5,037
  • 3
  • 31
  • 48
-1

Use this example to solve your problem happyBirthdayEmily

def happyBirthdayEmily(): #program does nothing as written
    print("Happy Birthday to you!")
    print("Happy Birthday to you!")
    print("Happy Birthday, dear Emily.")
    print("Happy Birthday to you!")

birthday3.py

'''Function definition and invocation.'''

def happyBirthdayEmily():
    print("Happy Birthday to you!")
    print("Happy Birthday to you!")
    print("Happy Birthday, dear Emily.")
    print("Happy Birthday to you!")

happyBirthdayEmily()
happyBirthdayEmily()

birthday4.py where we add a function happyBirthdayAndre, and call them both

'''Function definitions and invocation.'''

def happyBirthdayEmily():
    print("Happy Birthday to you!")
    print("Happy Birthday to you!")
    print("Happy Birthday, dear Emily.")
    print("Happy Birthday to you!")

def happyBirthdayAndre():
    print("Happy Birthday to you!")
    print("Happy Birthday to you!")
    print("Happy Birthday, dear Andre.")
    print("Happy Birthday to you!")

happyBirthdayEmily()
happyBirthdayAndre()

If we want the program to do anything automatically when it is runs, we need one line outside of definitions! The final line is the only one directly executed, and it calls the code in main, which in turn calls the code in the other two functions.