Reputation: 25144
Reading about Python types the description string-like
or byte-like
captured my attention. I couldn't find a proper definition what those exactly mean andhow to use those terms properly. I read for example in Python Cookbook
the following section:
basestring
is a common base class for thestr
andunicode
types, and any string-like type that user code might define should also subclassbasestring
, just to make sure that suchisinstance
testing works as intended.
So is it now correct to say that anything deriving from basestring
can be considered string-like? Even than what makes it string-like
, it's return type?
Upvotes: 0
Views: 388
Reputation: 1123550
<type>-like
is any object that acts just like that type. Python widely relies on duck typing; if it walks and quacks like a duck, it is a duck. If it behaves like a string, it probably is one.
So the documentation is telling you that if you are going to create a custom type that wants to be treated like a string everywhere, it may be an idea to subclass basestring
. That way any code that explicitly wants to test for string types (bytes or unicode text) by using isinstance(obj, basestring)
will accept your custom type too.
You still have to implement the minimum set of expected methods; inheriting from basestring
won't magically make your type work. That minimum set depends on how the code you are trying to dupe is manipulating the strings. Your type has to be string-like enough to dupe whatever you are using your type for.
Yes, that's deliberately vague, and not all code can be duped (some built-in functions and types expect to be able to use the C API on string objects, or even reach into the str
internals). Often it is easier to just subclass str
or unicode
to ensure nothing is missed.
The language has since stepped away from such base classes, and has defined abstract base classes instead, which, together with hooks for customising subclass and instance checks now are the recommended way to do duck typing in one check.
Upvotes: 4