0

I'm using the multiprocessing module on Python3.4. For some reason I get the following error using isinstance():

>>> from multiprocessing import Lock
>>> isinstance(Lock(), Lock)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: isinstance() arg 2 must be a type or tuple of types

Whereas if I try the same with datetime it works

>>> from datetime import datetime, timedelta
>>> isinstance(datetime.now(), datetime)

True

What I tried:

When I check see how Lock shows up in the console vs datetime and I get the following:

>>> Lock()
<Lock(owner=None)>
>>> Lock
<bound method DefaultContext.Lock of <multiprocessing.context.DefaultContext object at 0x00000000039810B8>>
>>> datetime.now()
datetime.datetime(2016, 6, 13, 11, 24, 12, 573712)
>>> datetime
<class 'datetime.datetime'>

but following Lock to its definition shows that it's indeed a class.

class Lock(object):
    def acquire(self, blocking=True, timeout=-1):
        pass

    def release(self):
        pass

So why does the console call it a "bound method". How can I use isinstance() on a Lock instance?

Jad S
  • 2,705
  • 6
  • 29
  • 49
  • 2
    That `Lock` is not a class. I'm not sure why you say it is – Andrea Corbellini Jun 13 '16 at 15:33
  • `bound method DefaultContext.Lock`... It's a method returning a Lock, not a class – mata Jun 13 '16 at 15:37
  • I'm using pyCharm. That class definition is what I get when I Ctrl+click on "Lock". It's in multiprocessing\\__init__.py – Jad S Jun 13 '16 at 15:45
  • What version of Python are you using? I checked from [3.0](https://hg.python.org/cpython/file/3.0/Lib/multiprocessing/__init__.py) to [the dev version](https://hg.python.org/cpython/file/default/Lib/multiprocessing/__init__.py), but could not find a `class Lock` inside `multiprocessing/__init__.py` – Andrea Corbellini Jun 13 '16 at 15:52
  • I'm using Python3.4.0 on PyCharm 4.5.4. The class definition is in PyCharm 4.5.4/helpers/python-skeletons/multiprocessing/__init__.py. – Jad S Jun 13 '16 at 15:56
  • @JadS Maybe there is something like that in PyCharm. But the output unambiguously says that the `Lock` in your code belongs to `multiprocessing.context.DefaultContext`, not to PyCharm. You can use `inspect` methods to check out its actual source code and its location. – ivan_pozdeev Jun 13 '16 at 16:22
  • Thank you everyone for looking into this – Jad S Jun 13 '16 at 18:08
  • You can add an answer to your own question – Andrea Corbellini Jun 13 '16 at 19:26

1 Answers1

0

It seems that my PyCharm IDE was leading me to a different definition of Lock than the one being interpreted when isinstance is run. The latter is a function. The Lock class I was looking for is defined in multiprocessing.synchronize.py

>>> from multiprocessing import Event, Lock, synchronize
>>> isinstance(Lock(), synchronize.Lock)
True
Jad S
  • 2,705
  • 6
  • 29
  • 49