What is the difference between isinstance(‘aaa’, basestring) and isinstance(‘aaa’, str)?

In Python versions prior to 3.0 there are two kinds of strings “plain strings” and “unicode strings”. Plain strings (str) cannot represent characters outside of the Latin alphabet (ignoring details of code pages for simplicity). Unicode strings (unicode) can represent characters from any alphabet including some fictional ones like Klingon. So why have two kinds … Read more

Does the default constructor initialize built-in types?

Implicitly defined (by the compiler) default constructor of a class does not initialize members of built-in types. However, you have to keep in mind that in some cases the initialization of a instance of the class can be performed by other means. Not by default constructor, nor by constructor at all. For example, there’s a … Read more

Can I add custom methods/attributes to built-in Python types?

You can’t directly add the method to the original type. However, you can subclass the type then substitute it in the built-in/global namespace, which achieves most of the effect desired. Unfortunately, objects created by literal syntax will continue to be of the vanilla type and won’t have your new methods/attributes. Here’s what it looks like … Read more