473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

assignment, references and list comprehension

Hi everyone

Was just posting a question as I got confused with a big messy sheaf of
code when I thought I should make a simple example myself. Since I did I
thought I'd post it for the good of mankind.

I was confused as to whether the assignment of a result of a list
comprehension created references to the orginal objects... and it does
(at least for my self-defined object).

e.g.
initArrrgs = ["pick me","not me"]

class MyObject:
def __init__(arg):
self.argument = arg;

myObjects = [MyObject(initarg) for initarg in initArrrgs]
myObjects2 = [ob for ob in myObjects if ob.argument == "pick me"]

Then the following interrogation
myObjects2[0].argument 'pick me' myObjects2[0].argument='juicy'
myObjects2[0].argument 'juicy' myObjects[0].argument

'juicy'
Jim
Jul 19 '05 #1
3 1858
"Jim O'D" wrote:
I was confused as to whether the assignment of a result of a list
comprehension created references to the orginal objects...
python never copies objects unless you tell it to, so the answer is yes. all
the "values" you pass around are object references, not binary blobs.
and it does (at least for my self-defined object).


trust me, it works the same way for all objects.

reading this may help:

http://effbot.org/zone/python-objects.htm

</F>

Jul 19 '05 #2
trust me, it works the same way for all objects.


Yes, it was lack of trust that led me on a 2 hour re-write to avoid
creating subsets of object lists as I thought they were being copied. In
fact it was another error... huh.

I now know better.

Jim
Jul 19 '05 #3
reading this may help:

http://effbot.org/zone/python-objects.htm

</F>


site bookmarked ;)
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
35
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you...
7
by: Chris P. | last post by:
Hi. I've made a program that logs onto a telnet server, enters a command, and then creates a list of useful information out of the information that is dumped to the screen as a result of the...
6
by: jena | last post by:
hello, when i create list of lambdas: l=] then l() returns 'C', i think, it should be 'A' my workaround is to define helper class with __call__ method: class X: def __init__(self,s): self.s=s...
6
by: MackS | last post by:
Hello everyone Consider the following >>> l = >>> for i in l: .... i = i + 1 .... >>> l
43
by: michael.f.ellis | last post by:
The following script puzzles me. It creates two nested lists that compare identically. After identical element assignments, the lists are different. In one case, a single element is replaced. In...
18
by: a | last post by:
can someone tell me how to use them thanks
4
by: Gregory Guthrie | last post by:
Sorry for a simple question- but I don't understand how to parse this use of a list comprehension. The "or" clauses are odd to me. It also seems like it is being overly clever (?) in using a...
1
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.