How to test for type or instance of dict_values?
fabian.becker87 at gmail.com
fabian.becker87 at gmail.com
Wed Dec 12 00:30:10 EST 2018
Very late to the party, but I just encountered a very similar problem and found a solution:
```
import collections
obj = {"foo": "bar"}
isinstance(obj.values(), collections.abc.ValuesView) # => True
```
Hope that helps someone out there :)
On Thursday, November 17, 2016 at 9:09:23 AM UTC-8, Terry Reedy wrote:
> On 11/17/2016 9:57 AM, Thorsten Kampe wrote:
>
> > The code in question is part of an attempt to get the dimensions of
> > multi-dimensional lists, the `isinstance` is there in order to
> > exclude strings.
>
> You can do the exclusion directly.
>
> >
> > """
> > def dim(seq):
> > dimension = []
> > while isinstance(seq, (list, tuple)):
>
> while not isinstance(seq, str) # or (str, bytes, ...)
>
> > dimension.append(len(seq))
> > try:
> > seq = seq[0]
> > except IndexError: # sequence is empty
> > break
> > return dimension
> > """
> >
> > Thorsten
> >
>
>
> --
> Terry Jan Reedy
More information about the Python-list
mailing list