In terms of what python is asked in an interview, it’s up to the interviewer.

Well, before you pull the knife, just get to the point.

In addition to a few common problems, most of the problems I encountered are things that you need to learn more about:

  • Common issues: for example, mutable/immutable data types, JSON serialization and deserialization, object reference/scope, common operations for dictionaries, and so on.
  • Additional knowledge: for example, Python’s memory management mechanism, shallow copy vs deep copy, ‘==’ vs ‘is’, etc.

Why do interviewers ask for extra knowledge? I think it depends on the depth of the candidate’s knowledge of the language. If you can settle down and read some Python books (AND I am one of those who can’t settle down), answering these questions is basically no problem.

I’m also looking through python books I bought a few years ago, and I’ll share some of those lessons later.

To start today, I think the most frequent interviews are: shallow copy vs deep copy, ‘==’ vs ‘is’

‘==’ vs ‘is’

Why do WE talk about this first, because we’re going to use object comparison later in the copy lesson.

First up:

  • The ‘==’ operator compares values between objects.
  • The ‘is’ operator compares the identity of objects for equality, that is, whether they are the same object and point to the same memory address.

Pay attention to

  • = =The operator is used to compare objectsvalueIs it equal?

Look at the example (running Python with command line interaction) :

λ python >>> A = 10 >>> B = 10 >>> A == B True >>> ID (a) 2407440149072 >>> ID (b) 2407440149072 >>> A is b True >>>Copy the code
  • You can see that there are two variables up here, a and B, both of which have a value of 10.
  • In Python, the identity of each object can be obtained through the function ID (object), so useisIs equivalent to comparing whether the ids of objects are equal.

In the result of the interactive run, you can see:

  • A == b, the result is True.
  • A is B, the result is also True.

Huh? Well, aren’t they the same?

Analyze:

  1. First, Python allocates a chunk of memory for the value 10.
  2. The variables A and B both point to this memory region.

So, so the values of A and B are the same, and id is the same.

However, for integer numbers, a is b is True and only applies to numbers in the range -5 to 256. Now try using 257:

>>> a = 257
>>> b = 257
>>> a is b
False
Copy the code

See the result is False.

The reason is that, for performance reasons, Python internally maintains an array of integers from -5 to 256, acting as a cache. Each time you try to create an integer number in the range -5 to 256, Python returns the corresponding reference from the array, rather than reinventing a new block of memory.

However, if integer numbers exceed this range, such as 257 in the example above, Python will have two memory areas for both 257, so a is B with different ids will return False.

In general, we use == more, because we care more about the values of two variables than about their internal storage addresses.

However, when checking whether the variable is None, we usually use is:

if a is None:
    ...

if a is not None:
    ...
Copy the code

Shallow copy vs deep copy

Don’t say depth first, copy everyone should be used.

>>> a = [1, 2, 3]
>>> b = list(a)
>>> b
[1, 2, 3]
>>> a == b
True
>>> a is b
False
Copy the code

B is a shallow copy of A, or you can slice the list: b = a[:].

You can also use copy, a function provided inside Python.

>>> import copy
>>> a = [1, 2, 3]
>>> b = copy.copy(a)
>>> b
[1, 2, 3]
>>> a is b
False
Copy the code

1. The shallow copy

Shallow copy: Reallocates a block of memory to create a new object with elements that are references to subobjects of the original object.

How to speak?

Take a look at a new example:

>>> import copy
>>> a = [1, 2, 3, 4, ['a', 'b']]
>>> b = copy.copy(a)
>>> a
[1, 2, 3, 4, ['a', 'b']]
>>> b
[1, 2, 3, 4, ['a', 'b']]
>>> a is b
False
Copy the code
  1. A is a list containing five elements, the fifth element of which is also a list [‘a’, ‘b’].
  2. B is a shallow copy of A, so both a and B will print out with equal values.
  3. If a is b is False, it means two different objects.

Notice that “the new object contains a reference to the subobject of the original object.” What about the reference to the child object?

Now I will modify A and add an element, which should not affect B.

>>> a.append(5)
>>> a
[1, 2, 3, 4, ['a', 'b'], 5]
>>> b
[1, 2, 3, 4, ['a', 'b']]
Copy the code

However, if I modify the child object [‘a’, ‘b’] in list A and add an element ‘c’, then look at what a and B are:

>>> a[4].append('c')
>>> a
[1, 2, 3, 4, ['a', 'b', 'c'], 5]
>>> b
[1, 2, 3, 4, ['a', 'b', 'c']]
Copy the code

As you can see, B is affected, and the list of b’s children has changed from [‘a’, ‘b’] to [‘a’, ‘b’, ‘c’].

So that explains the above, “Shallow copy, is to reallocate a block of memory and create a new object with elements that are references to the subobjects of the original object.”

This is also the problem with shallow copies, and it is important to pay attention to them in daily use.

2. Deep copy

Seeing this, you are probably smart enough to understand the power of deep copy. Yes, is to solve the problem of shallow copy, to achieve true full copy.

Deep copy: Reallocates a block of memory, creates a new object, and copies elements from the original object into the new object recursively by creating new child objects.

Therefore, the new object has no association with the original object.

To repeat the above example, use the deepcopy copy.deepcopy() :

>>> import copy
>>> a = [1, 2, 3, 4, ['a', 'b']]
>>> b = copy.deepcopy(a)
>>> a
[1, 2, 3, 4, ['a', 'b']]
>>> b
[1, 2, 3, 4, ['a', 'b']]
>>> a[4].append('c')
>>> a
[1, 2, 3, 4, ['a', 'b', 'c']]
>>> b
[1, 2, 3, 4, ['a', 'b']]
Copy the code

With deep copy, even if A changes child objects, B is not affected.