dictionary key type confusion isinstance python [duplicate]










0
















This question already has an answer here:



  • What are the differences between type() and isinstance()?

    6 answers



I can't understand following execution. I expected different results.



>>> f = 'ms':'ma'
>>> isinstance(f['ms'], type(str))
False

>>> isinstance(f['ms'], type(dict))
False

>>> type(f['ms'])
<class 'str'>









share|improve this question















marked as duplicate by MisterMiyagi, jpp dictionary
Users with the  dictionary badge can single-handedly close dictionary questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 11:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 5





    type(str) returns type, so you are checking if f['ms'] is an instance of type, not an instance of str. If you want to check if something is a string, use isinstance(f['ms'], str).

    – khelwood
    Nov 15 '18 at 11:42
















0
















This question already has an answer here:



  • What are the differences between type() and isinstance()?

    6 answers



I can't understand following execution. I expected different results.



>>> f = 'ms':'ma'
>>> isinstance(f['ms'], type(str))
False

>>> isinstance(f['ms'], type(dict))
False

>>> type(f['ms'])
<class 'str'>









share|improve this question















marked as duplicate by MisterMiyagi, jpp dictionary
Users with the  dictionary badge can single-handedly close dictionary questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 11:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 5





    type(str) returns type, so you are checking if f['ms'] is an instance of type, not an instance of str. If you want to check if something is a string, use isinstance(f['ms'], str).

    – khelwood
    Nov 15 '18 at 11:42














0












0








0









This question already has an answer here:



  • What are the differences between type() and isinstance()?

    6 answers



I can't understand following execution. I expected different results.



>>> f = 'ms':'ma'
>>> isinstance(f['ms'], type(str))
False

>>> isinstance(f['ms'], type(dict))
False

>>> type(f['ms'])
<class 'str'>









share|improve this question

















This question already has an answer here:



  • What are the differences between type() and isinstance()?

    6 answers



I can't understand following execution. I expected different results.



>>> f = 'ms':'ma'
>>> isinstance(f['ms'], type(str))
False

>>> isinstance(f['ms'], type(dict))
False

>>> type(f['ms'])
<class 'str'>




This question already has an answer here:



  • What are the differences between type() and isinstance()?

    6 answers







python dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 12:09







waltexwq

















asked Nov 15 '18 at 11:40









waltexwqwaltexwq

103




103




marked as duplicate by MisterMiyagi, jpp dictionary
Users with the  dictionary badge can single-handedly close dictionary questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 11:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by MisterMiyagi, jpp dictionary
Users with the  dictionary badge can single-handedly close dictionary questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 11:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 5





    type(str) returns type, so you are checking if f['ms'] is an instance of type, not an instance of str. If you want to check if something is a string, use isinstance(f['ms'], str).

    – khelwood
    Nov 15 '18 at 11:42













  • 5





    type(str) returns type, so you are checking if f['ms'] is an instance of type, not an instance of str. If you want to check if something is a string, use isinstance(f['ms'], str).

    – khelwood
    Nov 15 '18 at 11:42








5




5





type(str) returns type, so you are checking if f['ms'] is an instance of type, not an instance of str. If you want to check if something is a string, use isinstance(f['ms'], str).

– khelwood
Nov 15 '18 at 11:42






type(str) returns type, so you are checking if f['ms'] is an instance of type, not an instance of str. If you want to check if something is a string, use isinstance(f['ms'], str).

– khelwood
Nov 15 '18 at 11:42













2 Answers
2






active

oldest

votes


















4














type(str) and type(dict) each return type, so you are checking if your objects are instances of type, which they are not.



If you want to check if something is a string, use



isinstance(f['ms'], str)


not



isinstance(f['ms'], type(str))


And if you want to test if something is a dict, you can use



isinstance(f['ms'], dict)


not



isinstance(f['ms'], type(dict))





share|improve this answer
































    1














    I think you just want this:



    >>> f = 'ms':'ma'
    >>> isinstance(f['ms'], str)
    True


    You don't need type(str)






    share|improve this answer





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      type(str) and type(dict) each return type, so you are checking if your objects are instances of type, which they are not.



      If you want to check if something is a string, use



      isinstance(f['ms'], str)


      not



      isinstance(f['ms'], type(str))


      And if you want to test if something is a dict, you can use



      isinstance(f['ms'], dict)


      not



      isinstance(f['ms'], type(dict))





      share|improve this answer





























        4














        type(str) and type(dict) each return type, so you are checking if your objects are instances of type, which they are not.



        If you want to check if something is a string, use



        isinstance(f['ms'], str)


        not



        isinstance(f['ms'], type(str))


        And if you want to test if something is a dict, you can use



        isinstance(f['ms'], dict)


        not



        isinstance(f['ms'], type(dict))





        share|improve this answer



























          4












          4








          4







          type(str) and type(dict) each return type, so you are checking if your objects are instances of type, which they are not.



          If you want to check if something is a string, use



          isinstance(f['ms'], str)


          not



          isinstance(f['ms'], type(str))


          And if you want to test if something is a dict, you can use



          isinstance(f['ms'], dict)


          not



          isinstance(f['ms'], type(dict))





          share|improve this answer















          type(str) and type(dict) each return type, so you are checking if your objects are instances of type, which they are not.



          If you want to check if something is a string, use



          isinstance(f['ms'], str)


          not



          isinstance(f['ms'], type(str))


          And if you want to test if something is a dict, you can use



          isinstance(f['ms'], dict)


          not



          isinstance(f['ms'], type(dict))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 12:50

























          answered Nov 15 '18 at 11:46









          khelwoodkhelwood

          31.5k74364




          31.5k74364























              1














              I think you just want this:



              >>> f = 'ms':'ma'
              >>> isinstance(f['ms'], str)
              True


              You don't need type(str)






              share|improve this answer



























                1














                I think you just want this:



                >>> f = 'ms':'ma'
                >>> isinstance(f['ms'], str)
                True


                You don't need type(str)






                share|improve this answer

























                  1












                  1








                  1







                  I think you just want this:



                  >>> f = 'ms':'ma'
                  >>> isinstance(f['ms'], str)
                  True


                  You don't need type(str)






                  share|improve this answer













                  I think you just want this:



                  >>> f = 'ms':'ma'
                  >>> isinstance(f['ms'], str)
                  True


                  You don't need type(str)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 11:42









                  richflowrichflow

                  913211




                  913211













                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      政党