Skip to main content
replaced http://programmers.stackexchange.com/ with https://softwareengineering.stackexchange.com/
Source Link

Consider the concept of a closure (see e.g. this questionthis question and its answers). A closure saves you from passing around extra arguments that are known in a given context. For example, instead of writing:

def addTax(taxPercentage, amounts):
    def computeTax(perc, amount):
        return perc * amount / 100.0

    return [a + computeTax(taxPercentage, a) for a in amounts]

you can write

def addTax(taxPercentage, amounts):
    def computeTax(amount):
        return taxPercentage * amount / 100.0

    return [a + computeTax(a) for a in amounts]

The closure computeTax() implicitly gets the variable taxPercentage from its context (the context in which it is defined) so that it is not necessary to pass around the same variable again and again.

Now you have several functions that need to manipulate some common data. In fact, an object can be considered as a collection of closures that close over some common variables (the object's member variables). In this way, these functions need not pass their common data to each other explicitly.

So, yes, IMO it is definitely appropriate to make an object containing the data to be used during the computation and to implement all your functions as methods (closures) that access that common data.

Consider the concept of a closure (see e.g. this question and its answers). A closure saves you from passing around extra arguments that are known in a given context. For example, instead of writing:

def addTax(taxPercentage, amounts):
    def computeTax(perc, amount):
        return perc * amount / 100.0

    return [a + computeTax(taxPercentage, a) for a in amounts]

you can write

def addTax(taxPercentage, amounts):
    def computeTax(amount):
        return taxPercentage * amount / 100.0

    return [a + computeTax(a) for a in amounts]

The closure computeTax() implicitly gets the variable taxPercentage from its context (the context in which it is defined) so that it is not necessary to pass around the same variable again and again.

Now you have several functions that need to manipulate some common data. In fact, an object can be considered as a collection of closures that close over some common variables (the object's member variables). In this way, these functions need not pass their common data to each other explicitly.

So, yes, IMO it is definitely appropriate to make an object containing the data to be used during the computation and to implement all your functions as methods (closures) that access that common data.

Consider the concept of a closure (see e.g. this question and its answers). A closure saves you from passing around extra arguments that are known in a given context. For example, instead of writing:

def addTax(taxPercentage, amounts):
    def computeTax(perc, amount):
        return perc * amount / 100.0

    return [a + computeTax(taxPercentage, a) for a in amounts]

you can write

def addTax(taxPercentage, amounts):
    def computeTax(amount):
        return taxPercentage * amount / 100.0

    return [a + computeTax(a) for a in amounts]

The closure computeTax() implicitly gets the variable taxPercentage from its context (the context in which it is defined) so that it is not necessary to pass around the same variable again and again.

Now you have several functions that need to manipulate some common data. In fact, an object can be considered as a collection of closures that close over some common variables (the object's member variables). In this way, these functions need not pass their common data to each other explicitly.

So, yes, IMO it is definitely appropriate to make an object containing the data to be used during the computation and to implement all your functions as methods (closures) that access that common data.

Improved formulation
Source Link
Giorgio
  • 19.8k
  • 16
  • 87
  • 137

Consider the concept of a closure (see e.g. this question and its answers). A closure saves you from passing around extra arguments that are known in a given context. For example, instead of writing:

def addTax(taxPercentage, amounts):
    def computeTax(perc, amount):
        return perc * amount / 100.0

    return [a + computeTax(taxPercentage, a) for a in amounts]

you can write

def addTax(taxPercentage, amounts):
    def computeTax(amount):
        return taxPercentage * amount / 100.0

    return [a + computeTax(a) for a in amounts]

The closure computeTax() implicitly gets the variable taxPercentage from its context (the context in which it is defined) so that it is not necessary to pass around the same variable again and again.

Now you have several functions that need to manipulate some common data. In fact, an object can be considered as a collection of closures that close over some common variables (the object's member variables). In this way, these functions need not pass their common data to each other explicitly.

So, yes, IMO it is definitely appropriate to make an object containing the data to be used during the computation and to implement all your functions as methods (closures) that access that common data.

Consider the concept of a closure (see e.g. this question and its answers). A closure saves you from passing around extra arguments that are known in a given context. For example, instead of writing:

def addTax(taxPercentage, amounts):
    def computeTax(perc, amount):
        return perc * amount / 100.0

    return [a + computeTax(taxPercentage, a) for a in amounts]

you can write

def addTax(taxPercentage, amounts):
    def computeTax(amount):
        return taxPercentage * amount / 100.0

    return [a + computeTax(a) for a in amounts]

The closure computeTax() implicitly gets the variable taxPercentage from its context (the context in which it is defined) so that it is not necessary to pass around the same variable again and again.

Now you have several functions that need to manipulate some common data. In fact, an object can be considered as a collection of closures that close over some common variables (the object's member variables). So, yes, IMO it is definitely appropriate to make an object containing the data to be used during the computation and to implement all your functions as methods (closures) that access that common data.

Consider the concept of a closure (see e.g. this question and its answers). A closure saves you from passing around extra arguments that are known in a given context. For example, instead of writing:

def addTax(taxPercentage, amounts):
    def computeTax(perc, amount):
        return perc * amount / 100.0

    return [a + computeTax(taxPercentage, a) for a in amounts]

you can write

def addTax(taxPercentage, amounts):
    def computeTax(amount):
        return taxPercentage * amount / 100.0

    return [a + computeTax(a) for a in amounts]

The closure computeTax() implicitly gets the variable taxPercentage from its context (the context in which it is defined) so that it is not necessary to pass around the same variable again and again.

Now you have several functions that need to manipulate some common data. In fact, an object can be considered as a collection of closures that close over some common variables (the object's member variables). In this way, these functions need not pass their common data to each other explicitly.

So, yes, IMO it is definitely appropriate to make an object containing the data to be used during the computation and to implement all your functions as methods (closures) that access that common data.

Source Link
Giorgio
  • 19.8k
  • 16
  • 87
  • 137

Consider the concept of a closure (see e.g. this question and its answers). A closure saves you from passing around extra arguments that are known in a given context. For example, instead of writing:

def addTax(taxPercentage, amounts):
    def computeTax(perc, amount):
        return perc * amount / 100.0

    return [a + computeTax(taxPercentage, a) for a in amounts]

you can write

def addTax(taxPercentage, amounts):
    def computeTax(amount):
        return taxPercentage * amount / 100.0

    return [a + computeTax(a) for a in amounts]

The closure computeTax() implicitly gets the variable taxPercentage from its context (the context in which it is defined) so that it is not necessary to pass around the same variable again and again.

Now you have several functions that need to manipulate some common data. In fact, an object can be considered as a collection of closures that close over some common variables (the object's member variables). So, yes, IMO it is definitely appropriate to make an object containing the data to be used during the computation and to implement all your functions as methods (closures) that access that common data.