Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python decorator python

# class decorator 
import random

# changes the properties of a function to a class instance
class Eliphant:
    def __init__(self,funct):
        self._funct = funct
        #all return values are storredin momory
        self._memory = []

    def __call__(self):
        returnvalue = self._funct()
        self._memory.append(returnvalue)
        return returnvalue

    def memory(self):
        return self._memory


@Eliphant

def random_odd():
    return random.choice([1,3,5,7,9])
print(random_odd())
print(random_odd.memory())
print(random_odd())
print(random_odd.memory())
Source by book.pythontips.com #
 
PREVIOUS NEXT
Tagged: #python #decorator #python
ADD COMMENT
Topic
Name
4+4 =