Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

side effect in python with different variable types

Posted on 2014-07-24
8
Medium Priority
?
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 29

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 29

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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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 29

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 29

Accepted Solution

by:
pepr earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
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 …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

715 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