Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

side effect in python with different variable types

I think side effect is if value is mutated

s.count(s1) does not mutate string

for example string of length 5 'hello' will not become 5

Please explain side effect in more detail also with int, float, string, list, tuple, dictionary
0
rgb192
Asked:
rgb192
  • 4
  • 4
1 Solution
 
peprCommented:
The term side effect is not related to a type. It is related to implementation of a function. Strictly speaking, a function should return a value. If it modifies the environment in any other way, it may be called a side effect (say if it print something to standard output).

Object methods are sometimes called member functions. In their case, the side effect should be more relaxed in sense that they should or modify only the object variables or return some value.
0
 
rgb192Author Commented:
If side effect is only function,
how do lists have side effects?
0
 
peprCommented:
Lists are data structures. A side effect is always related to running a code of a function/method. I do not know about any side effect of the list-type methods (but I did not look closely).
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
rgb192Author Commented:
Is this a side effect of list

bigList1=[smallList1,2,3]

smallList1 gets mutated which has side effect of changing bigList1
0
 
peprCommented:
This is natural feature of a heterogeneous container where the "heterogeneous" means that each of the elements can be of a different type. The smallList1 is an object that does not know about the existence of bigList1. The bigList1 contains also the reference to the smallList1 object. However, it does not care what is the content of the smallList1.

Of course, both lists (the big and small) are mutable. Therefore serialization of the structure (say using of the repr() function) returns a different string representation of the list with another list inside. But changing the smallList1 intentionally changes the image of the whole structure. It is not a side effect. It is what we want.
0
 
rgb192Author Commented:
okay because list is mutable anyway

but tuple is not mutable

Is this a side effect of list

bigTuple1=(smallList1,2,3)

smallList1 gets mutated which has side effect of changing bigTuple1
0
 
peprCommented:
Tuple is immutable (not mutable) only in the sense it (the tuple itself) can never change its content. If a smallList1 was used to insert that reference to the tuple, then the list will be accessible through bigTuple1[0] and that cannot be changed. However, the content of the list can change, because it is mutable.

The Python illustrated (part 3) describes the case, and it later comes to hashability. When a tuple contains a list or other mutable object, then the logical content of the tuple can change and a has number (that represents a fixed content) cannot be calculated from it. However, when the tuple contains only immutable objects (recursively). then the content of the whole structure is constant (immutable) and the has can be calculated. In the later case, the tuple can be used as the key of a dictionary. In the earlier case, it cannot be used so.
0
 
rgb192Author Commented:
Ok, I need to learn more about hashes.

Thanks.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now