[issue33417] Isinstance() behavior is not consistent with the document

2018-05-03 Thread weapon

New submission from weapon :

In the PEP 3119(https://www.python.org/dev/peps/pep-3119/),it introduce about 
Customizing instance and subclass checks.

>The primary mechanism proposed here is to allow overloading the built-in 
>functions isinstance() and issubclass(). The overloading works as follows: The 
>call isinstance(x, C) first checks whether C.__instancecheck__ exists, and if 
>so, calls C.__instancecheck__(x) instead of its normal implementation.


but my code doesn't works.
```
class Sizeable(object):
def __instancecheck__(cls, instance):
print("__instancecheck__ call")
return hasattr(instance, "__len__")

class B(object):
pass

b = B()
print(isinstance(b, Sizeable)) # output:False
```
The __instancecheck__ function  is not called. In PyObject_IsInstance,when 
isinstance(x, C) the following three conditions are reached, __instancecheck__ 
will take effect:
1. x can not be directly instantiated by C;

2. The C class specifies the metaclass;

3. The __instancecheck__ in the specified metaclass class is defined.

This code can work:
```
class MetaSizeable(type):
def __instancecheck__(cls, instance):
print("__instancecheck__ call")
return hasattr(instance, "__len__")

class Sizeable(metaclass=MetaSizeable):
pass

class B(object):
pass

b = B()
print(isinstance(b, Sizeable))  # output: False
print(isinstance([], Sizeable)) # output: True
```

So,the problem is that the document does not conform to the reality.Can it be 
guaranteed that __instancecheck__  is always called?

If yes: PyObject_IsInstance should be tweaked.
If no:  document should be tweaked.

--
assignee: docs@python
components: Documentation, Interpreter Core
messages: 316118
nosy: docs@python, hongweipeng
priority: normal
severity: normal
status: open
title: Isinstance() behavior is not consistent with the document
type: behavior
versions: Python 3.7

___
Python tracker 
<https://bugs.python.org/issue33417>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue444582] Finding programs in PATH, adding shutil.which

2012-02-04 Thread Omega Weapon

Changes by Omega Weapon :


--
nosy: +Omega_Weapon

___
Python tracker 
<http://bugs.python.org/issue444582>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com