[SOLVED] Is it allowed to use 2 different languages ​​in a variable name?

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
span4ev
Regular
Posts: 105
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

[SOLVED] Is it allowed to use 2 different languages ​​in a variable name?

#1 Post by span4ev »

I checked this and it works. I didn't get an exception.
But here's what worries me: what if the exception doesn't occur now, but it will definitely cause a problem sometime later?

I understand that this is bad practice, and I know the rules for creating variable names in Python. But yesterday I struggled for a long time with one task that seemed impossible to me:

I needed to return a list of all class attributes in order to display the names of the characters in the info table through a loop but use a non-english language. So you need to take all the class attributes and somehow translate them into another language through a class method or use something like a dictionary that takes the name of a class attribute and translates it into another language.

Code: Select all

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

translation = {
    "name": "Имя",
    "age": "Возраст"
    }
And one person suggested to me that in Python, the name of a class attribute is allowed to use a language other than English.

Code: Select all

class Person:
    def __init__(self, name, age):

		self.Имя     = name
		self.Возраст = age
I didn't know about it until yesterday. It turned out to be very convenient and solved a lot of problems.

But I'm asking this question because I don't want to be 100% sure that all this will always work in Renpy.
Has anyone had a similar experience?

At the moment, I want to use the concatenation of variable names in order to take the names of class instances (in one language) from the dictionary using a cycle (for) and, based on them, create variables in English.

I doubt that someone really did this, but I'm still interested in it at the level of theory and depth of understanding of the possibilities
Last edited by span4ev on Sat Oct 15, 2022 5:04 am, edited 1 time in total.

User avatar
m_from_space
Miko-Class Veteran
Posts: 975
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Is it allowed to use 2 different languages ​​in a variable name?

#2 Post by m_from_space »

This feels a bit blasphemous, even though I am an atheist.

Variable names have no "language". It's just common to use English in the western world, so other programmers are able to read your code. I guess you refer to using character sets as part of variable names. Python2 supports only ASCII characters for variable names. Python3 supports unicode symbols. It's still not adviced to use random unicode characters if others should be able to use your code or whatever. Now of course you can use cyrillic letters for your variables, nobody will care.

But what's your goal here anyway? Variable names are things for programmers, compilers translate them into some memory addresses and forget about them (usually). Interpreters on the other hand may use them.

If you want to use another person's class and that class has some weird unicode letters as part of variable names, maybe just write a class, that copies the former one using your own variable names.

Maybe what you're doing here is way out of my scope, that's possible too.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Is it allowed to use 2 different languages ​​in a variable name?

#3 Post by Remix »

Please be aware that Python, though it supports unicode attribute and method names, will internally normalize them when reading the class ... this basically means you would need to rewrite the dunder getattr and setattr type methods to also normalize (unicodedata.normalize( "NFKC", unicode_name ) ) and even then make sure you are not duplicating ... e.g. If you had methods `yx` and `yₓ` the normalization would name both `yx`

Writing in your own language might be of benefit to you, just note that it will make things a lot harder for anyone not conversant in that language to offer help... All naming in Python should give immediate insight into what the variable, function or whatever relates to or does.


Yes, it is possible... do the benefits outweigh the detriments for you though?
Frameworks & Scriptlets:

span4ev
Regular
Posts: 105
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: Is it allowed to use 2 different languages ​​in a variable name?

#4 Post by span4ev »

m_from_space wrote: Wed Jul 13, 2022 1:17 pm This feels a bit blasphemous, even though I am an atheist.

Variable names have no "language". It's just common to use English in the western world, so other programmers are able to read your code. I guess you refer to using character sets as part of variable names. Python2 supports only ASCII characters for variable names. Python3 supports unicode symbols. It's still not adviced to use random unicode characters if others should be able to use your code or whatever. Now of course you can use cyrillic letters for your variables, nobody will care.

But what's your goal here anyway? Variable names are things for programmers, compilers translate them into some memory addresses and forget about them (usually). Interpreters on the other hand may use them.

If you want to use another person's class and that class has some weird unicode letters as part of variable names, maybe just write a class, that copies the former one using your own variable names.

Maybe what you're doing here is way out of my scope, that's possible too.
Sorry for answering so late. There was no notification and I didn't know that someone had answered the question. So I checked myself.

"But what's your goal here anyway? "
Since I don't know Python very well yet, I can't use complex and unknown approaches. But I need to move, experiment and practice. At the moment, I already got into a dead end so many times and spent 2-3 days on solving one problem. So most of the time I'm stuck in one place. I wanted to at least temporarily solve the issue of displaying the necessary attributes and their values ​​in the required language, move further in terms of writing code, and if anything, return to the problem of using Cyrillic in variables later, when I gain experience and knowledge.
Sometimes I have to use things like:

Code: Select all

buttion: 
	text class_attr_translate[list(current_class_name.__dict__.keys())[i]]
But this does not completely solve my problems. This is just a temporary crutch. I want to skip it at least temporarily. In addition, such lines are poorly readable and not intuitive.

I asked this question on another site, and there I was advised to either use a dictionary or use the language I need in the variable name.

The second option is very convenient and solves a lot of problems. Yes, I understand about code that other programmers will probably read, and that it's good practice to write variable names and function declarations using an international language. I fully support this. This is the correct approach. But I'm working on my own game alone, and not on a program that may become quite famous in the future. I just can't imagine what would have to happen for someone from another country to be interested in this code. This is extremely unlikely. Most likely only I will see this code. I don't have any other predictions.

"Interpreters on the other hand may use them."
Yes, that worried me too. I do not believe in myself and in my abilities, so I do not look so far into the future. But you are right. In this case, it's really better to stick to one language, because that will make translation impossible or very difficult.

"If you want to use another person's class and that class has some weird unicode letters as part of variable names, maybe just write a class, that copies the former one using your own variable names."
This is a good idea. I don't know how to implement it yet, but the idea is good. Thank you. I assume you mean something like this:

Code: Select all

class Person:
	def__init__(self, name)

		self.name = name

class Translate(Person):
	def __init__(self):
		super().__init__()
"Maybe what you're doing here is way out of my scope, that's possible too."

Anyway, you helped me a lot. You have answered the question and some advice. Thank you.

span4ev
Regular
Posts: 105
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: Is it allowed to use 2 different languages ​​in a variable name?

#5 Post by span4ev »

Remix wrote: Wed Jul 13, 2022 3:28 pm Please be aware that Python, though it supports unicode attribute and method names, will internally normalize them when reading the class ... this basically means you would need to rewrite the dunder getattr and setattr type methods to also normalize (unicodedata.normalize( "NFKC", unicode_name ) ) and even then make sure you are not duplicating ... e.g. If you had methods `yx` and `yₓ` the normalization would name both `yx`

Writing in your own language might be of benefit to you, just note that it will make things a lot harder for anyone not conversant in that language to offer help... All naming in Python should give immediate insight into what the variable, function or whatever relates to or does.


Yes, it is possible... do the benefits outweigh the detriments for you though?
Hello. Sorry for the late reply. There was no notification.

At this point in my development, knowledge of Python and advancement in coding - yes - the benefits outweigh the drawbacks.

"you would need to rewrite the dunder getattr and setattr type methods to also normalize (unicodedata.normalize( "NFKC", unicode_name ) ) "

Oh, thanks for the advice, I didn't even know about such nuances. I will study this later. Of course, I want to use only English language in the code, not because I expect someone else to be interested in the code, but because it's standards, it's good tone and correct code. But at this stage of development, this creates such difficulties that I have to stand still for several days, solving one problem.
Thank you for your advice and answer.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]