Solved

side effect in python with different variable types

Posted on 2014-07-24
8
238 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
Comment Utility
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
Comment Utility
If side effect is only function,
how do lists have side effects?
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
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
 

Author Comment

by:rgb192
Comment Utility
Is this a side effect of list

bigList1=[smallList1,2,3]

smallList1 gets mutated which has side effect of changing bigList1
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 28

Expert Comment

by:pepr
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ok, I need to learn more about hashes.

Thanks.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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 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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now