1

I've got 2 files : main.py and batsol.py

batsol.py contains a class and main.py is creating some instances from the class Batsol. So I'll show you a concise version of my code...

class Batsol:
  def __init__(self, addressCan = None, name = None) :
    self.addressCan = addressCan
    self.name = name
    #other stuff ...

Then my main.py :

from batsol import Batsol
# other import and code ...

print(callable(Batsol))
bs1 = Batsol()
# code...
if len(listener.ring_buffer) == 0 :
    for Batsol in tab_BS :
        try:
            print(tab_BS[Batsol])
        except (IndexError):
            pass
# code...
while(True) :
  # for and if interlocked
    print(callable(Batsol))
    bs2 = Batsol()

The console shows :

True
False
Traceback (most recent call last):
File "./main.py", line 135, in <module>
bs2 = Batsol()
TypeError: 'int' object is not callable

the second part of the traceback is not linked to other stuff i'm doing in my code (thread not terminated properly... something like this) , in my opinion

Exception ignored in: <module 'threading' from '/usr/lib/python3.4/threading.py'>
Traceback (most recent call last):
File "/usr/lib/python3.4/threading.py", line 1292, in _shutdown
t = _pickSomeNonDaemonThread()
File "/usr/lib/python3.4/threading.py", line 1300, in _pickSomeNonDaemonThread
if not t.daemon and t.is_alive():
TypeError: 'bool' object is not callable

WHY my object is not callable inside my tests loops ??? It drives me crazy...

Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
Lamoule74
  • 13
  • 3
  • np I understand :P – Lamoule74 May 27 '16 at 08:53
  • 1
    Are you sure there is no `Batsol = something` assignment somewhere in `#code ...` or `# for and if interlocked`? – Łukasz Rogalski May 27 '16 at 09:00
  • The most likely reason, given that we can't see what is in `code...`, is that something in there has assigned an `int` object to the name `Batsol`. You're going to think "well, obviously I wouldn't do that", but there's no way we can possibly know that, and one's opinion of one's own faulty code is always suspect. I can only suggest that you reduce the problem to a http://sscce.org/ If removing `code...` from your code makes the problem go away, then it's because of something in `code...`and barring psychic debugging we can't help you because we haven't seen the faulty code. – Steve Jessop May 27 '16 at 09:01
  • The error means that `Batsol` is an `int` at that point, and ints are not callable. As Steve said, you've assigned Batsol to be an int somewhere but not anywhere we can see. – SuperBiasedMan May 27 '16 at 09:04
  • Do you think a pastbin with the whole code could help? I understand traceback error not the origin... The fact is CTRL+F is my friend: I searched everywhere in my functions/files/classes imported in the main: no way to find what is modifying my Batsol object... I don't want to just paste the code and say "lets go find the error guys"... It's a little bit "easy".. – Lamoule74 May 27 '16 at 12:16
  • main.py: https://zerobin.net/?aa3826f57f95a65a#cvbdYzdXluHOTaW8GAPdW27/v5YOK9eKa+WoJoMLoGo= batsol.py: https://zerobin.net/?e5a37f130f991fb5#uGdbfREBxtJQjOrqAo42Zz6tAtdSnt07zYrw43tQIkQ= – Lamoule74 May 27 '16 at 12:26
  • @Lamoule74 Please try to trim down your code and edit your question to present an MCVE. Links may expire. – Łukasz Rogalski May 29 '16 at 08:36
  • OK I found the mistake: – Lamoule74 May 30 '16 at 07:10

1 Answers1

0

Your shadowing occurs in this code fragment:

if len(listener.ring_buffer) == 0 :
    for Batsol in tab_BS :
        try:
            print(tab_BS[Batsol])
        except (IndexError):
            pass
time.sleep(4)

for-in construct on sequences works as following:

  1. Sequence is asked for next (first, second, ... last) element. Internal pointer keeps track of element in current iteration.
  2. Element gets assigned to name on left side of "in".
  3. Go to 1.

After loop finishes, Batsol is no longer your class, but last element from tab_BS.

I'd suggest getting a better IDE, or using good static code analysis tool (Pylint / Flake8 etc.) as this kind of error is easily detected by e.g. PyCharm (your code shadows name from outer scope).

Related: How bad is shadowing names defined in outer scopes?

Community
  • 1
  • 1
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93