Reference gotcha

E

Ed Schofield

Hi all,
This interested me:
a1 = a2 = {}
a1['blah'] = 1
a2 {'blah': 1}
a1 == a2
True


Whereas ...

False

So how should one create a robust alias for a numeric type?

This bit me when I was trying to do the following in a class:
def __init__(self, data):
a = self.a = {}
b = self.b = {}
c = self.c = 0

# and increment them here ...

Best regards,
Ed Schofield
 
A

anton muhin

Ed said:
Hi all,
This interested me:

a1 = a2 = {}
a1['blah'] = 1
a2

{'blah': 1}

True


Whereas ...



False

So how should one create a robust alias for a numeric type?

This bit me when I was trying to do the following in a class:
def __init__(self, data):
a = self.a = {}
b = self.b = {}
c = self.c = 0

# and increment them here ...

Best regards,
Ed Schofield

Standard trick is to use single element list or just a small object with
integer field.

hth,
anton.
 
P

Paul Prescod

anton said:
Standard trick is to use single element list or just a small object with
integer field.

But why do that when "self." is already a small object with an integer
field?

Paul Prescod
 

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

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top