Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
When to check an object type - Python isinstance example
I'll encourage you not
to look at an object in a Python program - or in any other OO langauge - you find out what type it is ... at least until you've thought about why you're doing it.
If you need to, you can look - you have the type
function, you have the __class__.__name__
variable and you have isinstance
Isinstance lets you check the hierarcy of an object - so I can use it to ask if an object is of a particular type, or a type that's based on it.
Here's a look to see if something's a list:
and in use in a program [here]
. I'm not thrilled with the example - but is shows you the syntax. I would much rather you used polymorphism (several methods of the same name in order to decide internally how to print the data) rather than a branch chain based on the type.
Where I will
encourage you to use isinstance
is upon entry to a function that's called from outside your area of control - for example within the API of a function, method, module or package that you've written for others to use, and you want to check they're passing you sensible parameters. See [here]
for an example. (written 2016-11-03)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesY106 - Object Oriented Python 
Why populate object with values as you construct them? - (2016-02-18) 
From single block to structure and object oriented programming - (2015-12-02) 
What is the difference between a function and a method? - (2015-03-04) 
Simple OO demonstration in C++, comparison to Python - (2013-07-01) 
Really Simple Class and Inheritance example in Python - (2013-03-04) 
Spike solution, refactored and reusable, Python - Example - (2013-02-28) 
this or self - what are they, and what is the difference? (Python) - (2012-12-08) 
From Structured to Object Oriented Programming. - (2012-10-02) 
Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26) 
Moving from scripting to Object Orientation in Python - (2011-09-13) 
From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20) 
Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14) 
Tips for writing a test program (Ruby / Python / Java) - (2010-01-29) 
When should I use OO techniques? - (2009-05-11) 
Python - a truly dynamic language - (2009-01-30) 
Introduction to Object Oriented Programming - (2008-12-06) 
Screw it or Glue it? Access to Object variables - a warning - (2007-09-12) 
Python class rattling around - (2007-08-16) 
Python - function v method - (2006-10-20) 
Python makes University Challenge - (2006-08-15) 
Class, static and unbound variables - (2005-10-25)
Some other Articles
Writing Python 2 such that Python 3 is easyFrom and Import in Python - where is the module loaded from?Conditional operators in PythonEmbedding more complex code into a named blockWhen to check an object type - Python isinstance exampleCelebrating our diversity rather than discriminating for itNesting decoratorsDefining an object that is a modified standard type in Pythonwith in Python - examples of use, and of defining your own contextProfiling your Python program
4754 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).