download:Master Java project interview martial arts mind, interview curve overtaking

It is difficult to cover up your lack of technical or development experience. Most of the time, the interviewer does not care about what you have done, but more about your thinking, Angle and way of answering questions. Then how to answer the question, this course will give you the answer!

Suits the crowd

College students facing employment problems

Java engineers facing job hunting problems

Technical reserve requirements

Master Java basics

Have at least one simulation project development experience

New prices are added every day, so the average takes into account all the prices so far.

>>> AVG (10) 10.0 >>> AVG (20) 15.0 >>> > AVG (30) 20.0 2. Implementation method 1: Use class implementation

class Averager():

def __init__(self):
    self.series = []

def __call__(self, new_value):
    self.series.append(new_value)
    total = sum(self.series)
    return total/len(self.series)
Copy the code

avg = Averager()

Print (avg(10)) # print(avg(20)) # print(avg(20)) #

1, define a class variable save price, avg = Averager() class instantiation, generate instance object, create self.series, is an array.

2. Call magic method: Make avG instance object of Averager class callable.

Each time avG (ARG) is called, the list of class variables increases by one arG element and returns the average of the list sum.

3. Implementation method 2: use higher-order function implementation (pass the function as a parameter, such a function is called higher-order function)

def make_averager():

series = []

def averager(new_value):
    series.append(new_value)
    total = sum(series)
    return total/len(series)

return averager
Copy the code

avg = make_averager() print(avg.code.co_varnames) #(‘new_value’, ‘total’) print(avg. Code. co_freevars) #(‘series’,) print(AVg (10)) #10.0 print(AVg (20)) #15.0 print(avg(30)) #20.0

print(avg.closure) #(

,) print(avg.closure[0].cell_contents) #[10, 20, 30]

When make_averager() is called, an averager function object is returned. Each time averager is called, it adds parameters to the series of values and calculates the average.

Series is a local variable to the make_averager function

4. Similarities and differences between Method 1 and Method 2

The same:

Calling Averager() or make_averager() yields a callable avG, which updates the history value and then calculates the current mean.

We just call AVG (n), put n in the series of values, and recalculate the mean.

Different:

An instance of the Averager class, AVg, stores history values in self.series;

Where does the make_averager function look for series? Series is local to the make_averager function because series is initialized in the body of that function, series = []. However, when AVg (10) is called, the make_averager function has already returned, and its local scope is gone forever.

1. Closures and free variables: Based on implementation 2

The whole part is called a closure

series = [] def averager(new_value): Total = sum(series) return total/len(series)Copy the code

Description:

Averager’s closure extends beyond the scope of that function and contains bindings for free variables.

Func. Code print(avg.code.co_varnames) #(‘new_value’, ‘total’) returns function arguments and local variables as tuples

Print (avg.code.co_freevars) #(‘series’,)

Refer to:

Blog.csdn.net/jpch89/arti…

3, the func. Closure

Example 2

print(avg.closure) #(<cell at 0x00000151B2C415E8: list object at 0x00000151B2DA7AC8>,)

print(avg.closure[0].cell_contents) #[10, 20, 30]

Closure [cell.cell_contents for cell in func.closure] [cell.cell_contents for cell in func.closure]

Python 3 has renamed a function property named func_X to use the X form, freeing these names for user-defined properties in the function property namespace.

For example, func_closure, func_code, func_defaults, func_dict, func_doc, func_globals, and func_name are renamed closure, code, and de, respectively