Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was p[:] designed to work differently in these two situations?

p = [1,2,3] print(p) # [1, 2, 3]  q=p[:]  # supposed to do a shallow copy q[0]=11 print(q) #[11, 2, 3]  print(p) #[1, 2, 3]  # above confirms that q is not p, and is a distinct copy   del p[:] # why is this not creating a copy and deleting that copy ? print(p) # []  

Above confirms p[:] doesnt work the same way in these 2 situations. Isn't it ?

Considering that in the following code, I expect to be working directly with p and not a copy of p,

p[0] = 111 p[1:3] = [222, 333] print(p) # [111, 222, 333] 

I feel

del p[:]  

is consistent with p[:], all of them referencing the original list but

q=p[:]  

is confusing (to novices like me) as p[:] in this case results in a new list !

My novice expectation would be that

q=p[:] 

should be the same as

q=p 

Why did the creators allow this special behavior to result in a copy instead ?

like image 965
2020 Avatar asked Jun 26 '19 23:06

2020


People also ask

What are the benefits of product differentiation?

Product differentiation helps you refine your target audience as well. The more research you do and the more you differentiate, the better you'll understand who's actually buying your product or service. Then you can repeat the process to streamline your target audience even more.

What's the difference between moral hazard and adverse selection?

Adverse selection is the phenomenon that bad risks are more likely than good risks to buy insurance. Adverse selection is seen as very important for life insurance and health insurance. Moral hazard is the phenomenon that having insurance may change one's behavior. If one is insured, then one might become reckless.

What is product differentiation strategy?

Product differentiation is fundamentally a marketing strategy to encourage the consumer to choose one brand or product over another in a crowded field of competitors. It identifies the qualities that set one product apart from other similar products and uses those differences to drive consumer choice.

What are types of change management strategies?

Within directed change there are three different types of change management: developmental, transitional, and transformational.


1 Answers

del and assignments are designed consistently, they're just not designed the way you expected them to be. del never deletes objects, it deletes names/references (object deletion only ever happens indirectly, it's the refcount/garbage collector that deletes the objects); similarly the assignment operator never copies objects, it's always creating/updating names/references.

The del and assignment operator takes a reference specification (similar to the concept of an lvalue in C, though the details differs). This reference specification is either a variable name (plain identifier), a __setitem__ key (object in square bracket), or __setattr__ name (identifier after dot). This lvalue is not evaluated like an expression, as doing that will make it impossible to assign or delete anything.

Consider the symmetry between:

p[:] = [1, 2, 3] 

and

del p[:] 

In both cases, p[:] works identically because they are both evaluated as an lvalue. On the other hand, in the following code, p[:] is an expression that is fully evaluated into an object:

q = p[:] 
like image 172
Lie Ryan Avatar answered Sep 20 '22 06:09

Lie Ryan