Solved

side effect in python with different variable types

Posted on 2014-07-24
8
252 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

734 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