How to get name of a tuple inside of a tuple [duplicate] - python

This question already has answers here:
Convert Variable Name to String?
(23 answers)
Closed 1 year ago.
Let's say I have:
a = (1,2,3,4,5)
b = (2,4,5,6,7)
c = (a,b)
where a, b and c are tuples. How do I get the name of 'a' and 'b'?
I was trying with:
for x in c:
print(type(x).__name__)
but it gives me tuple

The python-varname package allows you to inspect variable names in general. Maybe that will help you. You can get it with pip install varname. Its GitHub is here and the README there provides usage examples.
edit: actually, for your use case, the comment given by #chepner is spot-on. I'll leave this answer up in case the package is of use to someone trying to get a variable name for a different case, since it would work on for example a and b themselves. But #chepner explains exactly why getting 'a' and 'b' out of 'c' is not going to be possible.

Related

What is actually internally happening in python when a=10, [duplicate]

This question already has answers here:
Why does adding a trailing comma after an expression create a tuple?
(6 answers)
Closed 1 year ago.
I am learning python where i come across below scenario
what actually happening internally in python can someone brief
My code :
a=10,
print(a)
Output coming as :
(10,)
a=10, is a tuple:
You can easily determine the type of the a using the type function as follows:
type(a)
Use a[0] to access the value 10:
a=10,
print(a[0])

Using a module with variable name in Python [duplicate]

This question already has answers here:
How to convert string to variable name?
(3 answers)
Closed 1 year ago.
I want to run a function of a module with a variable name for example:
Plugins.SomeUnknownName.function() without knowing their name but Plugins. and .function() stay the same.
I have tried several things like plugin = 'Plugins.' + unknown_plugin_name + '.function()' or
plugin_name = "SomePlugin"
Plugins.plugin_name.function()
But I don't know any further.
Maybe someone knows how to solve that.
Also this is my first stack overflow post, so please correct me if I did something wrong.
You can use getattr to pass the string name. Using numpy as an example here's what the normal usage would look like
>>> import numpy
>>> numpy.random.randint(1, 5)
2
Now similar to your example
>>> getattr(numpy, 'random').randint(1, 5)
2
So applying to your case
getattr(Plugins, 'plugin_name').function()

Cant print an variable associated to a string? [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 6 years ago.
i was playing around with python and was wondering why i cant print a variable thats associated with a string of text, this is what i wrote.
name = ("martim")
print name
Thanks for your help!
I hope these few lines will help you to understand how to use variables, values and print-statement in Python 2:
>>> name = "martin"
>>> print name
martin
>>> print "name"
name
>>> print "martin"
martin
Also you can take a look at the beginner's guide for Python: https://wiki.python.org/moin/BeginnersGuide
The proper way to perform what you're attempting to do is (note the comments are just to help understanding):
# Set name to "martin"
name = "martin"
# Print the value of name
print(name)
When assigning values to variables, parentheses shouldn't be used unless you're using a function for assignment; e.g., pi = float("3.14"). Print statements require parentheses as it is a function and the value being printed is the parameter.

How would one create variables in a loop? [duplicate]

This question already has answers here:
Dynamic variable in Python [duplicate]
(2 answers)
Closed 8 years ago.
I would like to know how to create lots of variables by looping it. I know other people have asked this before but everyone who knows says you need a good reason for it and to just set it in a dictionary. My reason is that I need to assign up to 6156119580207157310796674288400203776 variables and there is no way I can do that by typing them out.
I need something like:
while counter < 1000:
try[counter] = counter
So that I could do this:
>>> try837
837
>>>try453
453
etc.
(this is an example not the exact code but any answer for this will solve my problem)
I would also like to know why people are opposed to answering this particular question. I don't want to tax my computer more than I already am by assigning this many variables so if it is an issue that could harm my computer or my code I would like to know.
You don't want to do this. Create a dictionary with a key for each suffix that you would use. Then use try[557] in place of the variable try557.
>>> try_ = dict((counter, counter) for counter in range(1000))
>>> print try_[557]
557
I'm using the standard technique of affixing an underscore to the otherwise reserved word "try".
(I'm ignoring the ludicrously large number of variables you claim to need.)

How can I turn a regular string into a keyword? [duplicate]

This question already has answers here:
Converting String into Object Python
(2 answers)
Closed 9 years ago.
Recently, I have been programming in the language of Python. I've came across the problem of trying to convert a string into a keyword.
Suppose I use a raw_input() phrase and turn that string into an object, list, or dictionary.
For example, I can turn the string "Foo" into Foo and assign that name to a python structure.
How would I do this?
You don't want to do that, so instead of doing it, use a dictionary:
answer = raw_input("Enter: ") # Let's assume I enter "Foo"
mydict = {answer: raw_input("Enter a value for {} ".format(answer)} Let's say I enter "5"
print mydict.get('Foo')
# 5
You can modify the globals dictionary if you wish.
globals()[raw_input()] = None # whatever "structure" you desire.
print foo
The above code would only work if the user actually inputted the string 'foo'. Otherwise it would say NameError: name 'foo' is not defined.
But yeah like Haidro said, whatever you're trying to do this for is probably a silly idea.
As #icktoofay noted in the comments, this will only work with the return value of the built-in function globals(). Modifying the return value of the built-in function locals() will not actually modify the local namespace.

Categories

Resources