In Python2, strings of both types ( unicode and str ) were inherited from basestring . This was very useful when checking the types isinstance(_, basestring) . In Python3, the corresponding types of strings ( str and bytes ) are completely independent and have no common ancestor (what’s the logic?). Because of this, you have to write constructions like isinstance(_, (str, bytes)) , which is rather unaesthetic.
On the other hand, there are built-in types-characteristics, like collections.Iterable , which allow to check the value of a certain type of type through isinstance . For example, isinstance([], collections.Iterable) returns True , despite the fact that collections.Iterable missing from the ancestors of the type([]).mro() list.
Question: Is there a similar type of sign for checking all types of strings so that the condition is isinstance('', StringCategoty) and isinstance(b'', StringCategoty) ?
UPD: It is strange to hear that str and bytes supposedly have nothing in common with each other, while they implement an almost identical software interface: out of 44 ( str ) and 38 ( bytes ) their methods 36 are identical in their function and in the form of the call .
>>> str_methods = {method for method in dir(str) if not method.startswith('_')} >>> bytes_methods = {method for method in dir(bytes) if not method.startswith('_')} >>> shared_methods = str_methods.intersection(bytes_methods) >>> len(str_methods) 44 >>> len(bytes_methods) 38 >>> len(shared_methods) 36 The whole point of the differences between the two types of strings comes down to the fact that one is the encoded / decoded form of the other. Even their literals are written in an identical way and differ only in the presence of the prefix b . In addition, when working with different I / O libraries, we constantly have to deal with both types.
__next__and__iter__. The logic is that in one case you have a string for presenting textual information, and in another case a sequence of bytes. - Avernialdictandlist? Only that both of them are some kind of containers. To classify them on this basis, there arecollections.Iterable. Andstrandbytesare essentially a primitive "string" in encoded and decoded versions and have an almost identical API (I added this to the question). In practice, it is more often necessary to identify such primitives than to look for an indefinite kind of containers. Yes, and the principles of OOP require that the common interface be imposed in the form of an abstract class. - cridnirkstrandbytes? Only that both of them are some kind of containers. One stores characters, the other stores bytes. One in the access by index and in the loop produces lines of one character length, the other - numbers (yes, not evenbytes, butint). One can only be encoded, the other can only be decoded. They cannot be stacked with each other. Similarity is only external; technically, they have (almost) nothing in common and should not have any abstract basestring interfaces there. - andreymal