[Tutor] Another set question

Martin A. Brown martin at linux-ip.net
Fri Apr 28 22:42:36 EDT 2017


Hello and greetings Phil,

>> I'm trying to implement a conditional branch based on a variable type.
>
>This is often (not always) a mistake, in Python. So the question needs
>to be asked: What makes you think that condition is a requirement?
>
>So, I suspect you will need to explain better what larger problem you
>are trying to solve, and re-consider whether the condition you're trying
>to test is actually going to help that purpose.

I will echo what Ben has said.  As I thought about your question, I 
began to wonder more about what problem you were trying to solve 
than the question you are actually asking.

Here are some thoughts I can suggest.  We might be able to offer 
other suggestions if we know the problem you were tackling, 
although, of course, sometimes it is most fun/learning/challenging 
to discover these things for yourself.

Anyway, on to the thoughts:

  * Do you know about isinstance? [0] Be careful using this, as it 
    is easy to use it poorly.  Look up duck-typing [1] and see if 
    that helps in your solution.  The main point is to understand 
    what's possible with an object/instance not what type it is.

  * More basically, are you familiar with "in" keyword (reserved 
    word in Python)?  For any Python sequence, a list, tuple, set 
    and other such, you can test membership using "x in mysequence".
    Of course, it works on sequences, and strings are also sequences 
    (as you appear to have discovered), so this would require some 
    care.  See the docs for operations on sequences [2].  You might 
    be able to change your problem to using the "x in y" technique.

  * Finally, are you transforming your data before you operate on 
    it?  Based on your earlier question, I suspect you may not be 
    doing that.  One way to avoid headaches when you are operating 
    on data inside a program is to transform that data to the 
    expected datatypes as early as possible on input and then 
    transform that data as late as possible on output.

    Why do many programmers do this?  This means that inside the 
    program, you have some sort of contract about the datatypes on 
    which your program is operating.  There's no doubt about whether 
    a particular function is operating on an integer or a string.  
    For statically typed languages this is de rigeur.  For 
    dynamically typed languages like Python, you can elect not to do 
    this, but sometimes it will bite you, if you aren't careful. 
    Of course, you should take advantage of the dynamism, but 
    remember to consider either A) puttingthe data into an 
    internally canonical form before passing it around (my 
    preference) or writing your funtions / methods to deal with
    multiple datatypes.

>> For example, if c is a character and s is a set with only one member.
>
>Note that “a character” and “an empty string” and “three characters” are
>all values that have the exact same type: the ‘str’ type.
>
>Similarly, “a set with only one member” and “a set with two dozen
>members” and “an empty set” are all values that are of the same type:
>the ‘set’ type.

Simple example using "isinstance":

  # -- create a list of sets and one-character strings
  #
  l = [ set(("9")), "a", "b", "c", set(("q"))]
  
  # -- create a list with only the things that are of type set
  #
  q = [x for x in l if isinstance(x, set)]

  # Now, q = [{'9'}, {'q'}]

Example of using "in":

>In other words, why is the data such that you *need* to know whether it
>is a set versus a string, before using it? Can the data be handled
>differently such that the condition you describe isn't a prerequisite?

And, this is the key question.

Good luck!

-Martin

 [0] https://docs.python.org/3/library/functions.html#isinstance
 [1] http://www.voidspace.org.uk/python/articles/duck_typing.shtml
 [2] https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range

-- 
Martin A. Brown
http://linux-ip.net/


More information about the Tutor mailing list