[SOLVED] isinstance(var,str) always returning False

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Tess
Regular
Posts: 59
Joined: Thu Aug 04, 2022 3:43 pm
Projects: The Songbird Guild
Organization: Yurisoft
Github: wainwt2
Discord: Tess#7782
Contact:

[SOLVED] isinstance(var,str) always returning False

#1 Post by Tess »

I have a function I'm trying to use to make **kwargs in some of our functions not case sensitive. This is what I have so far.

Code: Select all

def dict_lower(dictionary):
        """Takes a dict with string keys and makes them all lower case"""
        for key,value in dictionary.items():
            if isinstance(key,str):
                if key.lower() not in dictionary:
                    dictionary[key.lower()] = value
                    dictionary.pop(key)
        return dictionary
Right now this does nothing, however if I comment out "if isinstance(key,str)" it works perfectly.

Code: Select all

#This works perfectly
def dict_lower(dictionary):
        """Takes a dict with string keys and makes them all lower case"""
        for key,value in dictionary.items():
            #if isinstance(key,str):
            if key.lower() not in dictionary:
                dictionary[key.lower()] = value
                dictionary.pop(key)
        return dictionary
I can't figure out why isinstance doesn't want to recognize key as a string, when it seems to be behaving as one everywhere else.
Last edited by Tess on Thu Dec 01, 2022 12:46 pm, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2400
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: isinstance(var,str) always returning False

#2 Post by Ocelot »

How do you call that function?
And try to print actual object class before checking it. Then look in console to see what do you have. If you ever see something strange, usually you want to add a ton of print statements displaying everything you might need about processes that happen inside.
< < insert Rick Cook quote here > >

User avatar
Tess
Regular
Posts: 59
Joined: Thu Aug 04, 2022 3:43 pm
Projects: The Songbird Guild
Organization: Yurisoft
Github: wainwt2
Discord: Tess#7782
Contact:

Re: isinstance(var,str) always returning False

#3 Post by Tess »

I added "print key" and "print type(key)" to the beginning of the for loop. For each one it printed the string fine and then <type 'str'> after it. So key seems to be a string, isinstance just doesn't want to accept it :?

I also added print "Made it here" right inside isinstance and it never printed...

Code: Select all

def dict_lower(dictionary):
        """Takes a dict with string keys and makes them all lower case"""
        for key,value in dictionary.items():
            print key #This prints the string fine
            print type(key) #This prints <type 'str'>
            if isinstance(key,str):
            	print "Made it here" #This never prints
                if key.lower() not in dictionary:
                    dictionary[key.lower()] = value
                    dictionary.pop(key)
        return dictionary
Really baffling...

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2400
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: isinstance(var,str) always returning False

#4 Post by Ocelot »

Are you on RenPy 7.5 by any chance?
< < insert Rick Cook quote here > >

User avatar
Tess
Regular
Posts: 59
Joined: Thu Aug 04, 2022 3:43 pm
Projects: The Songbird Guild
Organization: Yurisoft
Github: wainwt2
Discord: Tess#7782
Contact:

Re: isinstance(var,str) always returning False

#5 Post by Tess »

Yup!

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2400
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: isinstance(var,str) always returning False

#6 Post by Ocelot »

Try to check for (str, pystr, future.utils.text_type) - that's it, tuple of three types. IIRC RenPy replaces standard string as part of Python3 compatibility fixes.

If it works, figure out concrete type you want. My bet is on pystr.
< < insert Rick Cook quote here > >

User avatar
Tess
Regular
Posts: 59
Joined: Thu Aug 04, 2022 3:43 pm
Projects: The Songbird Guild
Organization: Yurisoft
Github: wainwt2
Discord: Tess#7782
Contact:

Re: isinstance(var,str) always returning False

#7 Post by Tess »

You were exactly right, pystr did the trick. Thanks! :)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]