scope, modyfing outside object from inside the method

  • Thread starter =?iso-8859-2?q?Marcin_St=EApnicki?=
  • Start date
?

=?iso-8859-2?q?Marcin_St=EApnicki?=

Hello.

I thought I understand this, but apparently I don't :(. I'm missing
something very basic and fundamental here, so redirecting me to the
related documentation is welcomed as well as providing working code :).

Trivial example which works as expected:
x = {'a':123, 'b': 456}
y = x
x['a']=890
y
{'a': 890, 'b': 456}

Now, let's try something more sophisticated (it's not real world example,
I've made up the problem which I think illustrates my issue). Let's say
I've got such a structure:

results = [ {'a': 12, 'b': 30 },
{'a': 13, 'b': 40 } ]

I'd like to have each row and column in separate object of self-made
classes.:

class mycolumn():
def __init__(self, resultset, row, col):
self.value = resultset[row][col]
def __str__(self):
return 'Column value: %s' % self.value

class myrow():
def __init__(self):
self.container = {}
def __str__ (self):
return self.container

results = [
{'a': 12, 'b' :30 },
{'a': 13, 'b' :40 }
]

mystruct = []

for row in results:
mystruct.append ( myrow() )
for col in row:
mystruct [len(mystruct)-1].container[col] = \
mycolumn(results, results.index(row), col)

print mystruct[0].container['b'] # 12
results[0]['b'] = 50 #
print mystruct[0].container['b'] # also 12 :/

In other words, I'd like to "map" the results to myrow and mycolumn
objects, and have these new objects' values changed when I change "results".

I hope I explained it well enough :). Thank you for your time.
 
P

Peter Otten

Marcin said:
Hello.

I thought I understand this, but apparently I don't :(. I'm missing
something very basic and fundamental here, so redirecting me to the
related documentation is welcomed as well as providing working code :).

Trivial example which works as expected:
x = {'a':123, 'b': 456}
y = x
x['a']=890
y
{'a': 890, 'b': 456}

Now, let's try something more sophisticated (it's not real world example,
I've made up the problem which I think illustrates my issue). Let's say
I've got such a structure:

results = [ {'a': 12, 'b': 30 },
{'a': 13, 'b': 40 } ]

I'd like to have each row and column in separate object of self-made
classes.:

class mycolumn():
def __init__(self, resultset, row, col):
self.value = resultset[row][col]
def __str__(self):
return 'Column value: %s' % self.value

class myrow():
def __init__(self):
self.container = {}
def __str__ (self):
return self.container

results = [
{'a': 12, 'b' :30 },
{'a': 13, 'b' :40 }
]

mystruct = []

for row in results:
mystruct.append ( myrow() )
for col in row:
mystruct [len(mystruct)-1].container[col] = \
mycolumn(results, results.index(row), col)

print mystruct[0].container['b'] # 12
results[0]['b'] = 50 #
print mystruct[0].container['b'] # also 12 :/

In other words, I'd like to "map" the results to myrow and mycolumn
objects, and have these new objects' values changed when I change "results".

Instead of copying a value, you can tell your objects where to look it up.
Your mycolumn class would then become

class MyColumn(object):
def __init__(self, resultset, row, col):
self._resultset = resultset
self._row = row
self._col = col
@property
def value(self):
return self._resultset[self._row][self._col]
def __str__(self):
return 'Column value: %s' % self.value

Peter
 
D

Dustan

@property
def value(self):
return self._resultset[self._row][self._col]

I remember a thread where someone created a version of property that
worked like this, but it's not in the standard python release, unless
it is in python 3000.
 
G

Guest

Marcin said:
Hello.

I thought I understand this, but apparently I don't :(. I'm missing
something very basic and fundamental here, so redirecting me to the
related documentation is welcomed as well as providing working code :).

Trivial example which works as expected:
x = {'a':123, 'b': 456}
y = x
x['a']=890
y
{'a': 890, 'b': 456}

Now, let's try something more sophisticated (it's not real world example,
I've made up the problem which I think illustrates my issue). Let's say
I've got such a structure:

results = [ {'a': 12, 'b': 30 },
{'a': 13, 'b': 40 } ]

I'd like to have each row and column in separate object of self-made
classes.:

class mycolumn():
def __init__(self, resultset, row, col):
self.value = resultset[row][col]
def __str__(self):
return 'Column value: %s' % self.value

class myrow():
def __init__(self):
self.container = {}
def __str__ (self):
return self.container

results = [
{'a': 12, 'b' :30 },
{'a': 13, 'b' :40 }
]

mystruct = []

for row in results:
mystruct.append ( myrow() )
for col in row:
mystruct [len(mystruct)-1].container[col] = \
mycolumn(results, results.index(row), col)

print mystruct[0].container['b'] # 12
results[0]['b'] = 50 #
print mystruct[0].container['b'] # also 12 :/

In other words, I'd like to "map" the results to myrow and mycolumn
objects, and have these new objects' values changed when I change "results".

I hope I explained it well enough :). Thank you for your time.

Would this work for you?

class myrow():
def __init__(self, idict = {}):
self.container = idict
def __str__ (self):
return self.container.__str__()

results = [
{'a': 12, 'b' :30 },
{'a': 13, 'b' :40 }
]

mystruct = []

for row in results:
mystruct.append(myrow(row))

results[1]['b'] = 444

print results # [{'a': 12, 'b': 30}, {'a': 13, 'b': 444}]

print mystruct # does not work ok , you should probably define
# mystruct's __str__ method properly

# But, save for the __str__ thingy, the rest is ok.

for row in mystruct:
print row

# {'a': 12, 'b': 30}
# {'a': 13, 'b': 444}

HTH
 
?

=?iso-8859-2?q?Marcin_St=EApnicki?=

Dnia Mon, 24 Sep 2007 10:41:22 -0300, Ricardo Aráoz napisał(a):
Would this work for you?

Thank you both for help. Well - yes and no :). It's getting more
interesting:

First, your code:

class myrow():
def __init__(self, idict = {}):
self.container = idict
def __str__ (self):
return self.container.__str__()

results = [
{'a': 12, 'b' :30 },
{'a': 13, 'b' :40 }
]

mystruct = []

for row in results:
mystruct.append(myrow(row))

results[1]['b'] = 444

print results # [{'a': 12, 'b': 30}, {'a': 13, 'b': 444}]

print mystruct # does not work ok , you should probably define
# mystruct's __str__ method properly

# But, save for the __str__ thingy, the rest is ok.

for row in mystruct:
print row

# {'a': 12, 'b': 30}
# {'a': 13, 'b': 444}

And now let's try to swap results with something else:

print id(results)

# new resultset
results = [
{'a': 112, 'b' : 130 },
{'a': 113, 'b' : 140 }
]

print id(results)
for row in mystruct:
print row

# {'a': 12, 'b': 30}
# {'a': 13, 'b': 444}

At first glance (before adding id()) it's a little bit weird. The original
object was supposedly "overwritten", but as one can see it has different
id then the new one. mystruct still holds references to old object,
though. I can handle it and just change original object, but I'm curious
if there's a way to "swap" them "on-the-fly".

Thanks one again.
 
G

Gabriel Genellina

En Wed, 26 Sep 2007 10:39:22 -0300, Marcin Stępnicki
At first glance (before adding id()) it's a little bit weird. The
original
object was supposedly "overwritten", but as one can see it has different
id then the new one. mystruct still holds references to old object,
though. I can handle it and just change original object, but I'm curious
if there's a way to "swap" them "on-the-fly".

You should read this <http://effbot.org/zone/python-objects.htm>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top