isdsucph / isds2022

Introduction to Social Data Science 2022 - a summer school course https://isdsucph.github.io/isds2022/
MIT License
21 stars 23 forks source link

Code only works after resetting kernel #27

Open simull opened 2 years ago

simull commented 2 years ago

Hi,

we were working on a task 3.3.3.4: of assignment 1 and produced the following code:

def my_func(element): 
    res1 = tuple(element.split('-'))
    res2 = tuple([int(i) for i in res1])
    return res2
my_func('1-11')

This is a code I also produced and it always returned an Error. However, the same code works for the others in my group. It only worked for me, when I restarted the kernel. How should I know that I have to restart the kernel and stop changing and working on the code that should already work?

I got the following Error :

TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_3364/4279762030.py in <module>
      3     res2 = tuple([int(i) for i in res1])
      4     return res2
----> 5 my_func('1-11')

~\AppData\Local\Temp/ipykernel_3364/4279762030.py in my_func(element)
      1 def my_func(element):
----> 2     res1 = tuple(element.split('-'))
      3     res2 = tuple([int(i) for i in res1])
      4     return res2
      5 my_func('1-11')

TypeError: 'tuple' object is not callable

Best, Simon

isdsucph commented 2 years ago

Hi @simull

Looks a bit weird. Could it be that you have defined tuple to be equal to some tuple and therefore when you apply tuple to the list it raises an error? Try to send the whole notebook of yours for context :)

Cheers Jonas

simull commented 2 years ago

Hi Jonas,

thanks for your response. I don't think it has anything to do with the code, because after I restarted the kernel the same code went through. So after restarting the kernel, I can no loner reproduce the issue and without error, it is hard to look why that happened.

Best, Simon

isdsucph commented 2 years ago

Hi @simull

It depends on what you have defined the variables in your (global) namespace to be. It could be that before you restarted your kernel you wrote somewhere in a cell that

tuple = tuple(list("ABCDEF"))

Then the variable tuple, which normally has the builtin function tuple assigned to it, would have a tuple object assigned instead of the builtin function. When you apply tuple to the list in your function it will raise an TypeError saying that tuple is not callable, because tuple is not the builtin function but a tuple object. If you then restart your kernel the global namespace will reset and tuple will again become the builtin function. With the global namespace reset to its default (and without running the line of code above) your function will work without any problems. At least this is what I think is the most likely explanation to your now disappeared error :)