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 ?
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.
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.
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.
Within directed change there are three different types of change management: developmental, transitional, and transformational.
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[:]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With