Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Identifying object types
#1
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")
Output:
Obj type of txt_colour = <class 'tuple'> size = 3

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)
Output:
Obj type of parameter txt_colour = <class 'tuple'> size = 3
Reply


Messages In This Thread
Identifying object types - by microphone_head - Oct-01-2017, 07:52 AM
RE: Identifying object types - by snippsat - Oct-01-2017, 11:12 AM
RE: Identifying object types - by microphone_head - Oct-01-2017, 11:47 AM
RE: Identifying object types - by buran - Oct-01-2017, 12:21 PM
RE: Identifying object types - by microphone_head - Oct-01-2017, 01:57 PM
RE: Identifying object types - by buran - Oct-01-2017, 02:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Identifying if the program I have is python and then decompiling jpnyc 7 2,427 Jun-02-2022, 10:16 PM
Last Post: jpnyc
  Identifying keywords in text drchips 6 110,016 Mar-29-2022, 12:32 PM
Last Post: snippsat
  trying to put a a filter on identifying a straight CompleteNewb 1 1,697 Dec-01-2021, 11:11 PM
Last Post: CompleteNewb
  Identifying string success flag graham23s 4 3,196 Aug-14-2019, 09:27 PM
Last Post: graham23s
  identifying a dictionary with an attribute? Skaperen 7 3,894 Oct-04-2018, 05:48 AM
Last Post: Skaperen
  Identifying only specific words in a string GilbyScarChest 2 2,737 Aug-08-2018, 03:22 AM
Last Post: GilbyScarChest
  Identifying the value of all adjacent elements in an array JoeB 2 8,687 Nov-23-2017, 05:10 PM
Last Post: JoeB

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020