- ✕Tá an achoimre seo ginte ag intleacht shaorga atá bunaithe ar roinnt foinsí ar líne. Úsáid na naisc "Foghlaim tuilleadh" chun amharc ar an mbunfhaisnéis fhoinseach.
In Python, variables are essentially references to objects, and objects themselves do not inherently store information about the variable names pointing to them. However, there are several techniques to retrieve the name of a variable based on its value. These methods rely on inspecting the current scope or using custom wrappers.
Using locals() or globals()
The locals() and globals() functions return dictionaries of variables in the local and global scopes, respectively. By iterating through these dictionaries, you can find the variable name that points to a specific value.
Example with locals():
def get_variable_name(variable):variable_name = [name for name, value in locals().items() if value is variable][0]return variable_namex = 42print(get_variable_name(x)) # Output: xCóipeáilte!✕CóipeáilExample with globals():
def get_variable_name(variable):variable_name = [name for name, value in globals().items() if value is variable][0]return variable_namey = "Hello"print(get_variable_name(y)) # Output: yCóipeáilte!✕CóipeáilIs maith liom Ní maith liom python - Getting the name of a variable as a string - Stack Overflow
25 Lún 2013 · Variables don't "have names"; "variable" means the same thing as "name" in Python. Presumably you meant value (as in, the actual object that is being named); but values don't …
- Athbhreithnithe: 4
Sampla de chód
""" Get Variable Name as String by comparing its ID to globals() Variables' IDsargs:variable(var): Variable to find name for (Obviously this variable has to exist)kwargs:globalVariables(dict): Copy of the globals() dict (Adding to Kwargs allows this function to work properly when imported from another .py)...How To Print A Variable's Name In Python - GeeksforGeeks
10 Noll 2025 · Python does not provide a built-in way to retrieve a variable’s name directly, but with some clever approaches, it is possible to identify a variable by its reference in the code.
Unveiling the Mystery: How to Get the Name of a Variable in Python
13 Aib 2025 · Understanding these methods can be extremely useful in debugging, logging, and more complex programming tasks where variable names carry important semantic …
Python - Variable Names - W3Schools
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)
Convert String into Variable Name in Python - GeeksforGeeks
25 MFómh 2023 · There may be situations where you want to convert a string into a variable name dynamically. In this article, we'll explore how to convert a string into a variable name in …
Get Variable Name As String In Python - GeeksforGeeks
1 Iúil 2025 · Getting a variable name as a string in Python means finding the label that points to a specific value. For example, if x = "Python", we can loop through globals () or locals () and …
- Iarrann daoine freisin
Python: Getting Variable Names - CodeRivers
13 Aib 2025 · However, Python doesn't provide a straightforward built - in way to directly get the variable name in all cases. This blog post will explore different techniques to achieve this, along …
What is the naming convention in Python for variables and …
Variable names follow the same convention as function names. mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards …
python - How create variable name with other variable's value in it ...
12 Ean 2022 · See the linked dupe: use a dict. Variable variables are possible in python, but if you need to ask how to do them, you don't need them (there's a vanishingly small set of cases …
Python Naming Conventions For Variables
19 Márta 2025 · Learn Python naming conventions for variables, including practices for snake_case, constants, and private variables. This step-by-step guide includes examples.