2

My dictionary has the following structure:

a = 'stringA'
b = 'stringB'
c = 'stringC'

endpoints = {
    'foo1': a + 'string1' + b,
    'foo2' : a + 'string2' + c + 'string3' + b,
}

My problem is the following: when I call endpoints['foo2'], I get the expected array value. However, when I change the value of, for instance, c between the array declaration and the usage of endpoints['foo2'], the value of c is not updated.

Any idea of why this happens and how can it be solved?

PS: I know this could be done creating a simple function, but I think that would be quite more inefficient.

8
  • 3
    Thats because your array does not save a reference to c Commented Mar 28, 2016 at 9:48
  • @JeD There are no arrays here - or at all in python (unless you count numpy arrays and other non-native modules). endpoints is a dictionary. Commented Mar 28, 2016 at 9:51
  • @Alvaro Gomez "when I change the value of, for instance, c". Try posting the code you use to change the value of c Commented Mar 28, 2016 at 9:53
  • 1
    @KevinGuan yep Strings are immutable. You can get around it by defining a own class or putting the string into a list. Commented Mar 28, 2016 at 9:59
  • 1
    @JeD There no lists either :P But yeah, you're right. The values are fixed/immutable as soon as endpoints is created and it would take a re-definition of the particular entry to change it. @Alvaro would have to do c='newString' followed by endpoints['foo2'] = a + 'string2' + c + 'string3' + b or use a lambda function like Kevin Guan suggested. Commented Mar 28, 2016 at 10:06

1 Answer 1

2

You could do this:

a = ['stringA']
b = ['stringB']
c = ['stringC']

endpoints = {
    'foo1': a ,
    'foo2' : b
}

print(endpoints['foo1']) #returns "[stringA]"

a[0]='otherString'

print(endpoints['foo1']) #returns "[otherString]"

You can do this, because you can change the values in a list without changing the reference;

a and endpoints are still using the same space for a

This is not possible for pure Strings, because you can not change them without a new assignment. Strings in Python are immutable.

Edit: Another possibility would be to create your own string class.

This removes the [] brackets:

class MyStr:

    def __init__(self,val):
        self.val=val

    def __repr__(self):
    #this function is called by dict to get a string for the class
        return self.val

    def setVal(self,val):
        self.val=val

a=MyStr("abcd")
b={1:a}
print(b) #prints {1:"abcd"}
a.setVal("cdef")
print(b) #prints {1:"cdef"}

Disclaimer: As explained in the comments I am still using python 2.7

While Python 3 and 2.7 are mostly compatible, there might be some smaller bugs when trying to use this.

Sign up to request clarification or add additional context in comments.

9 Comments

Aren't you getting TypeError: can only concatenate list (not "str") to list anyway?​​​​​​​​​​​​​​​ This is Python, not JavaScript or other programming languages like it.
@KevinGuan oh yeah, dammit
print is a function in nowadays Python.
It's not that the code is incorrect, @Kevin. It's just that it's written for Python 2.
@UlrichEckhardt But it'll usually work just fine in Python2 with the parentheses as well. You run the risk of inadvertently creating a tuple though. from __future__ import print_function if you want to be on the safe side.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.