Solved

side effect in python with different variable types

Posted on 2014-07-24
8
245 Views
Last Modified: 2014-07-29
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
Comment
Question by:rgb192
  • 4
  • 4
8 Comments
 
LVL 28

Expert Comment

by:pepr
ID: 40217901
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
 

Author Comment

by:rgb192
ID: 40218273
If side effect is only function,
how do lists have side effects?
0
 
LVL 28

Expert Comment

by:pepr
ID: 40218624
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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:rgb192
ID: 40220447
Is this a side effect of list

bigList1=[smallList1,2,3]

smallList1 gets mutated which has side effect of changing bigList1
0
 
LVL 28

Expert Comment

by:pepr
ID: 40220487
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
 

Author Comment

by:rgb192
ID: 40220647
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
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
ID: 40221355
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
 

Author Closing Comment

by:rgb192
ID: 40228167
Ok, I need to learn more about hashes.

Thanks.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question