-1

I have following test,

def test_strings_concatenation(self):
    dict = ['a', 'b', 'c']
    dict_as_string = " ".join(dict)
    expected = 'a b c'
    assert dict_as_string is expected

and I want to get dict exactly, (identical to) expected. Is there any way to get this?

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
Kuba Wenta
  • 580
  • 1
  • 5
  • 25
  • 6
    `assert dict_as_string == expected`. This is not a job for `is`. (Alternately, if you're asking if there's a way to make `is` truthy in this scenario... no.) Also, try not to use variable names that are pre-defined in Python, like `dict`. See [Is there a difference between “==” and “is”?](https://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is) – Amadan May 10 '19 at 08:25
  • 2
    `is` will check if two object point to the same place in memory. Here `dict_as_string` and `dict` are two separate objects and therefore `is` will return `False`. – Tomasz Bartkowiak May 10 '19 at 08:27
  • `dict = ['a', 'b', 'c']` is extremely misleading. `dict` is the built-in for _dictionary_ – Jean-François Fabre May 10 '19 at 08:31
  • Oh, yes, sorry for use dict as name. – Kuba Wenta May 10 '19 at 08:41
  • @TomaszBartkowiak well I have checked that if I create two different object with the same value it gives me equality. ` def test_strings_comp(self): a1='a' a2='a' assert a1 is a2 ` – Kuba Wenta May 10 '19 at 08:45
  • @TomaszBartkowiak correct me if I am wrong, but what the OP found is a special behaviour, pointing to https://stackoverflow.com/questions/50037548/python-is-operator-behaviour-with-string – Devesh Kumar Singh May 10 '19 at 09:17
  • 1
    @KubaWenta That is because Python caches small integers so they effectively are the same object, e.g for `a = 'a'` and `b = 'a'` `a is b` yields `True`. – Tomasz Bartkowiak May 10 '19 at 15:51

1 Answers1

1

To begin with, never use pre-defined constants like dict as variable names as @Amadan pointed out, also ['a', 'b', 'c'] is a list, and not a dictionary, (which is a container to hold key-value pairs e.g. {"a":"b"}.

Also you would want to check for == since you want the list to be concatenated to a string, and not is, since is checks if two object refers to the same location of memory as @TomaszBartkowiak pointed out , like below

In [21]: a = 1   

In [22]: b = a                                                                                                                                                                    

In [23]: a is b                                                                                                                                                                   
Out[23]: True

In [24]: li = ['a', 'b', 'c']                                                                                                                                                     

In [25]: s = ' '.join(li)                                                                                                                                                         

In [26]: s is li                                                                                                                                                                  
Out[26]: False

Hence the code will change to

def test_strings_concatenation():
    #Define list and concatenate it
    li = ['a', 'b', 'c']
    li_as_string = " ".join(li)
    expected = 'a b c'
    #Check for string equality
    assert li_as_string == expected

test_strings_concatenation()
Sheldore
  • 37,862
  • 7
  • 57
  • 71
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40