The Borderless Tourist

So, I’m at step50 on this and it seems that I did what was sought a couple steps ahead of time and without intention. So, in this code …

def find_attractions(destination, interests):
  destination_index = get_destination_index(destination)
  print("\n====== ln" + str(inspect.currentframe().f_lineno) + ", destination_index: ", end="")
  print(destination_index)

  attractions_in_city = attractions[destination_index]
  print("\n====== ln" + str(inspect.currentframe().f_lineno) + ", List of attractions in {destination}:".format(destination=destination))
  print(attractions_in_city)

  print("\n====== ln" + str(inspect.currentframe().f_lineno) + ", traveler interests:")
  print(interests)

  for attraction in attractions_in_city:
    attractions_with_interests = []
    possible_attraction, attraction_tags = attraction
    # possible_attraction = attraction
    # attraction_tags = attraction[1]

    for interest in interests:
      if interest in attraction_tags:
        attractions_with_interests.append(possible_attraction)
        print("\n====== ln" + str(inspect.currentframe().f_lineno) + ", List of possible attractions:")
        print(interest)
        print(attractions_with_interests)
  return attractions_with_interests

# print(find_attractions(*test_traveler))
la_arts = find_attractions("Los Angeles, USA",["art"])
print("\n====== ln" + str(inspect.currentframe().f_lineno) + ", la_arts:")
print(la_arts)

… what I want to know is: Why does unpacking …

possible_attraction, attraction_tags = attraction

returns:
([‘LACMA’])

while this …

    possible_attraction = attraction
    attraction_tags = attraction[1]

returns:
[[‘LACMA’, [‘art’, ‘museum’]]]

Without seeing the rest of the code its hard to say for certain what you have as there appears to be more than one level of nested list. I’m not quite sure what you mean by returns, if its the overall function return then you should note how certain kinds of sequence can be unpacked and assigned to variables-

Unpacking values in this way works like the following-

my_list = [0, 1]
a, b = my_list
print('a is {}, b is {}'.format(a, b))
Out: a is 0, b is 1

So your two chunks of code assign a very different object to the name possible_arraction which would then affect the output.

1 Like

You could also place tracer statements in your code. In theory the unpacking should be the same, but if it’s behaving differently it’s for a logical reason (you just have to find it). It’s good form since it’ll make you realize what’s working cleanly in your code and what’s not.

I don’t think there’s anything revealing that would, but trying not to reveal spoilers for other students, this is my full code to this point. The ONLY change made in the code is in either unpacking or assigning the attraction[1] data.

There has to be something about how unpacking handles the data differently than assignment. It seems to unlink the paired information but maintains a pointer such that when possible_attraction is appended to attractions_with_interest the only data appended is attraction[0].

Oh dear, me. I have zero clue what you refer to, much less how to implement it.

Oh yeah, it is behaving exactly the way it’s supposed to (just not how I expected) and i just don’t understand why.

Oh hells, bells. I just figured it out.

Yeah, the unpacking separates the data AND assignment would do that too IF I had separated the data the same way.

Nevermind. I figured it out.

In the assignment code, I do

possible_attraction = attraction <== NOTE: the UNindexed value.

This assigns the WHOLE attraction data structure, not just attraction[1] as unpacking implicitly does.

By tracers statements, I mean leaving formatted print statements in strategic locations that help one trace the trajectory of a variable or code in general. It can be good for catching things that the compiler doesn’t.

Ah, I think I’m already doing something like that.

Do these qualify?

I’m new but my google-fu is fairly good and I found out how to embed lineno (line number) on which of code executes by importing inspect.