get variable name of instance [duplicate] - python

This question already has answers here:
Simpler way to create dictionary of separate variables?
(27 answers)
How can you print a variable name in python? [duplicate]
(8 answers)
Closed 8 years ago.
Can a Python instance get the name of the variable which is used to access the object?
This example code shows what I need:
foo=MyClass()
foo.get_name() --> 'foo'
bar=foo
bar.get_name() --> 'bar'
I know that this is black magic and not clean code. I just want to know if it is possible.
I know that bar.__name__ returns the name, but I need it inside an own method.
How can get_name() be implemented?
This is not a duplicate of questions which answer is __name__

Related

Pass a str variable for flag in Python function? [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Alternatives for returning multiple values from a Python function [closed]
(14 answers)
Closed 1 year ago.
In python want to do something like this:
def func(*args, flag):
do something
if cond A:
flag="some A"
else:
flag="some B"
Then I found the flag passed is always unchanged.
If I change the flag to be a list, then yes it works but is ugly.
What is the proper way to achieve the above?

Python: Assign value to non-existing field [duplicate]

This question already has answers here:
How can I create an object and add attributes to it?
(18 answers)
How do I create a Python namespace (argparse.parse_args value)?
(4 answers)
Closed 3 years ago.
a = None
a.s = 2
throws an AttributeError. Is there a possibility to create an object a with the field s with value 2 without creating an own class for that? Would be super cool if Python would allow that hack! 8)

Why does python let you access loop variables out of scope [duplicate]

This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 3 years ago.
I wrote something like this where I am able to access a variable that is used through pythons in for loop syntax and then being able to access that variable, I was wondering if there is a specific reason to being able to do this
for i in array:
do_something(i)
b = i
From Short description of the scoping rules?: "The for loop does not have its own namespace"
This means that variables declared in the loop are available for the rest of the code in the same scope as the loop itself

How to get all class, variables, func names in current namescope? [duplicate]

This question already has an answer here:
What's the difference between globals(), locals(), and vars()?
(1 answer)
Closed 4 years ago.
How can I get all class, object, function names in current namescope in Python? How can I differentiate between them(e.g. get only class names)?
Use help() or dir().
You can dump the output into a list of strings and read from there too to differentiate what you require

How can i convert a string in a python method? [duplicate]

This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Closed 8 years ago.
I receive a string, for example "save". And i have a method save with paramethers.
How can i convert the string save in a call to save().
I tried with eval and exec.
Edit: Solved here --> Calling a function of a module from a string with the function's name in Python
def some_method(self):
save_method = getattr(self, 'save')
save_method() # save()

Categories