Identifying object types - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Identifying object types (/thread-5384.html) |
Identifying object types - microphone_head - Oct-01-2017 Can anyone out there suggest a better way of using the "type" routine? I am trying to improve my code by identifying errors. In addition to using the "try" command I would like to identify variable types. The idea is to identify the object type of a parameter passed by a calling routine. I have been using the "type" routine, but believe there must be a better way. I am runing Python 3 on my Raspberry Pi2. def add_option(mnu_caption, txt_colour =None): if txt_colour is None: # set the menu item to its default colour. txt_colour =(255, 255, 255) else: # verify parameter is valid if "tuple" in type(txt_colour): if len(txt_colour) !=3: # we got a problem pass print("Obj type of txt_colour =", type(txt_colour), " size =", len(txt_colour)) add_option("Run Game")
Sorry guys and girls I made a mistake on the previous submission of the code. Here's the revised copy of the code. Note the keyword "str" when testing the "type". def add_option(mnu_caption, txt_colour =None): if txt_colour is None: # set the menu item to its default colour. txt_colour =(255, 255, 255) else: # verify parameter is valid if "tuple" in str(type(txt_colour)): if len(txt_colour) !=3: # we got a problem pass print("Obj type of parameter txt_colour =", type(txt_colour), " size =", len(txt_colour)) txtcol =(255, 255, 255) add_option("Run Game", txtcol)
RE: Identifying object types - snippsat - Oct-01-2017 It's better to use isinstance() than type() for this. >>> t = (100,200,150) >>> if isinstance(t, tuple) and len(t) == 3: ... print("It's most likely a rgb color tuple") ... else: ... print('Not a color tuple') ... It's most likely a rgb color tuple >>> t = [100,200,155] >>> if isinstance(t, tuple) and len(t) == 3: ... print("It's most likely a rgb color tuple") ... else: ... print('Not a color tuple') ... Not a color tuple >>> t = (100,200) >>> if isinstance(t, tuple) and len(t) == 3: ... print("It's most likely a rgb color tuple") ... else: ... print('Not a color tuple') ... Not a color tuple RE: Identifying object types - microphone_head - Oct-01-2017 That's just what I was looking for. Thanks snippsat. RE: Identifying object types - buran - Oct-01-2017 why do you need to check the type? https://en.wikipedia.org/wiki/Duck_typing in this particular case if user supply a 3-element list, it would serve the same as 3-element tuple (if not t for the test)... by the way, tuple is not mutable, so you can safely supply default RGB tuple in the function signature def add_option(mnu_caption, txt_colour=(255, 255, 255)): RE: Identifying object types - microphone_head - Oct-01-2017 I am writing a class object that allows a calling routine to specify a text colour. My reasoning is that if the calling rountine passes an invalid parameter it can cause a runtime error. I could trap it with the [try] but I want to be specific. I haven't completed my homework on raising errors. Most of my code contains very little error checking to prevent runtime errors, so Im updating it. My aim is to get in the habbit of writing code that will cause the least amount of trouble when ported onto different platforms. Thanks for the heads up though, MH. RE: Identifying object types - buran - Oct-01-2017 if this is a class it's even more logical to rise TypeError if what you get is not what you expect. in your view how the calling routine would know that what it supply is not what your class expect? |