How to capture a value in a Python Closure - python-3.x

How does one capture a value (as opposed to a reference) in a python closure?
What I have tried:
Here is a function which makes a list of parameter-less functions, each of which spits out a string:
def makeListOfFuncs( strings):
list = []
for str in strings:
def echo():
return str
list.append( echo)
return list
If closures in python worked like every other language, I would expect that a call like this:
for animalFunc in makeListOfFuncs( ['bird', 'fish', 'zebra']):
print( animalFunc())
... would yield output like this:
bird
fish
zebra
But in python instead, we get:
zebra
zebra
zebra
Apparently what is happening is that the closure for the echo function is capturing the reference to str, as opposed to the value in the call frame at the time of closure construction.
How can I define makeListOfFuncs, so that I get 'bird', 'fish', 'zebra'?

I found the answer here on a blog post by Bob Kerner.
def makeListOfFuncs( strings):
list = []
for str in strings:
def generateFunc( str):
def echo():
return str
return echo
list.append( generateFunc( str))
return list
for animalFunc in makeListOfFuncs( ['bird', 'fish', 'zebra']):
print( animalFunc())
Python closures are weird.

Python closure is not weird if you know how closure internally works in python.
def makeListOfFuncs( strings):
list = []
for str in strings:
def echo():
return str
list.append( echo)
return list
You are returning a list of closures. variable "str" is shared between scopes, it is in two different scopes. When python sees it, it creates an intermediary object. this object contains a reference to another object which is "str". and for each closure, it is the same cell. You can test it:
closures_list= makeListOfFuncs( ['bird', 'fish', 'zebra'])
# Those will return same cell address
closures_list[0].__closure__
closures_list[1].__closure__
closures_list[2].__closure__
When you call makeListOfFuncs, it will run the for-loop and at the end of the loop, the intermediary object will point to "zebra". So when you call each closure print( animalFunc()) , it will visit the intermediary obj which will reference to "zebra".
You have to think about what happens when python creates a function and when it evaluates it.
def echo():
return str
We need to somehow able to capture the value of "str" as the function being created. Because if you wait until function gets evaluated, then it is too late. Another solution would be defining a default value:
def makeListOfFuncs( strings):
list = []
for str in strings:
def echo(y=str):
return y
list.append( echo)
return list
for animalFunc in makeListOfFuncs( ['bird', 'fish', 'zebra']):
print( animalFunc())
This solution works because default values get evaluated at creation time. So when echo is created, the default value of "str" will be used. the default value will not point to the cell.
in the first iteration, the default value will be "bird". Python will not see this as a free variable. in this case we are not even creating closure, we are creating function. in next iteration "fish" and in the last iteration "zebra" will be printed

Related

Why along with output it is showing NONE each time? [duplicate]

What does the return statement do? How should it be used in Python?
How does return differ from print?
See also
Often, people try to use print in a loop inside a function in order to see multiple values, and want to be able to use the results from outside. They need to be returned, but return exits the function the first time. See How can I use `return` to get back multiple values from a loop? Can I put them in a list?.
Often, beginners will write a function that ultimately prints something rather than returning it, and then also try to print the result, resulting in an unexpected None. See Why is "None" printed after my function's output?.
Occasionally in 3.x, people try to assign the result of print to a name, or use it in another expression, like input(print('prompt:')). In 3.x, print is a function, so this is not a syntax error, but it returns None rather than what was displayed. See Why does the print function return None?.
Occasionally, people write code that tries to print the result from a recursive call, rather than returning it properly. Just as if the function were merely called, this does not work to propagate the value back through the recursion. See Why does my recursive function return None?.
Consider How do I get a result (output) from a function? How can I use the result later? for questions that are simply about how to use return, without considering print.
The print() function writes, i.e., "prints", a string in the console. The return statement causes your function to exit and hand back a value to its caller. The point of functions in general is to take in inputs and return something. The return statement is used when a function is ready to return a value to its caller.
For example, here's a function utilizing both print() and return:
def foo():
print("hello from inside of foo")
return 1
Now you can run code that calls foo, like so:
if __name__ == '__main__':
print("going to call foo")
x = foo()
print("called foo")
print("foo returned " + str(x))
If you run this as a script (e.g. a .py file) as opposed to in the Python interpreter, you will get the following output:
going to call foo
hello from inside foo
called foo
foo returned 1
I hope this makes it clearer. The interpreter writes return values to the console so I can see why somebody could be confused.
Here's another example from the interpreter that demonstrates that:
>>> def foo():
... print("hello within foo")
... return 1
...
>>> foo()
hello within foo
1
>>> def bar():
... return 10 * foo()
...
>>> bar()
hello within foo
10
You can see that when foo() is called from bar(), 1 isn't written to the console. Instead it is used to calculate the value returned from bar().
print() is a function that causes a side effect (it writes a string in the console), but execution resumes with the next statement. return causes the function to stop executing and hand a value back to whatever called it.
Think of the print statement as causing a side-effect, it makes your function write some text out to the user, but it can't be used by another function.
I'll attempt to explain this better with some examples, and a couple definitions from Wikipedia.
Here is the definition of a function from Wikipedia
A function, in mathematics, associates one quantity, the argument of the function, also known as the input, with another quantity, the value of the function, also known as the output..
Think about that for a second. What does it mean when you say the function has a value?
What it means is that you can actually substitute the value of a function with a normal value! (Assuming the two values are the same type of value)
Why would you want that you ask?
What about other functions that may accept the same type of value as an input?
def square(n):
return n * n
def add_one(n):
return n + 1
print square(12)
# square(12) is the same as writing 144
print add_one(square(12))
print add_one(144)
#These both have the same output
There is a fancy mathematical term for functions that only depend on their inputs to produce their outputs: Referential Transparency. Again, a definition from Wikipedia.
Referential transparency and referential opaqueness are properties of parts of computer programs. An expression is said to be referentially transparent if it can be replaced with its value without changing the behavior of a program
It might be a bit hard to grasp what this means if you're just new to programming, but I think you will get it after some experimentation.
In general though, you can do things like print in a function, and you can also have a return statement at the end.
Just remember that when you use return you are basically saying "A call to this function is the same as writing the value that gets returned"
Python will actually insert a return value for you if you decline to put in your own, it's called "None", and it's a special type that simply means nothing, or null.
I think the dictionary is your best reference here
Return and Print
In short:
return gives something back or replies to the caller of the function while print produces text
In python, we start defining a function with def, and generally - but not necessarily - end the function with return.
Suppose we want a function that adds 2 to the input value x. In mathematics, we might write something like f(x) = x + 2, describing that relationship: the value of the function, evaluated at x, is equal to x + 2.
In Python, it looks like this instead:
def f(x):
return x + 2
That is: we define a function named f, which will be given an x value. When the code runs we figure out x + 2, and return that value. Instead of describing a relationship, we lay out steps that must be taken to calculate the result.
After defining the function, it can be called with whatever argument you like. It doesn't have to be named x in the calling code, and it doesn't even have to be a variable:
print f(2)
>>> 4
We could write the code for the function in some other ways. For example:
def f(x):
y = x + 2
return y
or even
def f(x):
x = x + 2
return x
Again, we are following steps in order - x = x + 2 changes what x refers to (now it means the result from the sum), and that is what gets returned by return x (because that's the value *at the time that the return happens).
return means "output this value from this function".
print means "send this value to (generally) stdout"
In the Python REPL, a function's return value will be output to the screen by default (this isn't the same as printing it). This output only happens at the REPL, not when running code from a .py file. It is the same as the output from any other expression at the REPL.
This is an example of print:
>>> n = "foo\nbar" #just assigning a variable. No output
>>> n #the value is output, but it is in a "raw form"
'foo\nbar'
>>> print(n) #the \n is now a newline
foo
bar
>>>
This is an example of return:
>>> def getN():
... return "foo\nbar"
...
>>> getN() #When this isn't assigned to something, it is just output
'foo\nbar'
>>> n = getN() # assigning a variable to the return value. No output
>>> n #the value is output, but it is in a "raw form"
'foo\nbar'
>>> print(n) #the \n is now a newline
foo
bar
>>>
This answer goes over some of the cases that have not been discussed above.
The return statement allows you to terminate the execution of a function before you reach the end. This causes the flow of execution to immediately return to the caller.
In line number 4:
def ret(n):
if n > 9:
temp = "two digits"
return temp #Line 4
else:
temp = "one digit"
return temp #Line 8
print("return statement")
ret(10)
After the conditional statement gets executed the ret() function gets terminated due to return temp (line 4).
Thus the print("return statement") does not get executed.
Output:
two digits
This code that appears after the conditional statements, or the place the flow of control cannot reach, is the dead code.
Returning Values
In lines number 4 and 8, the return statement is being used to return the value of a temporary variable after the condition has been executed.
To bring out the difference between print and return:
def ret(n):
if n > 9:
print("two digits")
return "two digits"
else :
print("one digit")
return "one digit"
ret(25)
Output:
two digits
'two digits'
Note that return can also be used for control flow. By putting one or more return statements in the middle of a function, we can say: "stop executing this function. We've either got what we wanted or something's gone wrong!"
For example, imagine trying to implement str.find(sub) if we only had str.index(sub) available (index raises a ValueError if the substring isn't found, whereas find returns -1).
We could use a try/except block:
def find(s: str, sub: str) -> int:
try:
return s.index(sub)
except ValueError:
return -1
This is fine, and it works, but it's not very expressive. It's not immediately clear what would cause str.index to raise a ValueError: a reader of this code must understand the workings of str.index in order to understand the logic of find.
Rather than add a doc-string, saying "...unless sub isn't found, in which case return -1", we could make the code document itself, like this:
def find(s: str, sub: str) -> int:
if sub not in s:
return -1
return s.index(sub)
This makes the logic very clear.
The other nice thing about this is that once we get to return s.index(sub) we don't need to wrap it in a try/except because we already know that the substring is present!
See the Code Style section of the Python Guide for more advice on this way of using return.
To put it as simply as possible:
return makes the value (a variable, often) available for use by the caller (for example, to be stored by a function that the function using return is within). Without return, your value or variable wouldn't be available for the caller to store/re-use.
print, by contrast, prints to the screen - but does not make the value or variable available for use by the caller.
Difference between "return" and "print" can also be found in the following example:
RETURN:
def bigger(a, b):
if a > b:
return a
elif a <b:
return b
else:
return a
The above code will give correct results for all inputs.
PRINT:
def bigger(a, b):
if a > b:
print a
elif a <b:
print b
else:
print a
NOTE: This will fail for many test cases.
ERROR:
----
FAILURE: Test case input: 3, 8.
Expected result: 8
FAILURE: Test case input: 4, 3.
Expected result: 4
FAILURE: Test case input: 3, 3.
Expected result: 3
You passed 0 out of 3 test cases
Here is my understanding. (hope it will help someone and it's correct).
def count_number_of(x):
count = 0
for item in x:
if item == "what_you_look_for":
count = count + 1
return count
So this simple piece of code counts number of occurrences of something. The placement of return is significant. It tells your program where do you need the value. So when you print, you send output to the screen. When you return you tell the value to go somewhere. In this case you can see that count = 0 is indented with return - we want the value (count + 1) to replace 0.
If you try to follow logic of the code when you indent the return command further the output will always be 1, because we would never tell the initial count to change.
I hope I got it right.
Oh, and return is always inside a function.
return should be used for recursive functions/methods or you want to use the returned value for later applications in your algorithm.
print should be used when you want to display a meaningful and desired output to the user and you don't want to clutter the screen with intermediate results that the user is not interested in, although they are helpful for debugging your code.
The following code shows how to use return and print properly:
def fact(x):
if x < 2:
return 1
return x * fact(x - 1)
print(fact(5))
This explanation is true for all of the programming languages not just python.
return is part of a function definition, while print outputs text to the standard output (usually the console).
A function is a procedure accepting parameters and returning a value. return is for the latter, while the former is done with def.
Example:
def timestwo(x):
return x*2
Best thing about return function is you can return a value from function but you can do same with print so whats the difference ?
Basically return not about just returning it gives output in object form so that we can save that return value from function to any variable but we can't do with print because its same like stdout/cout in C Programming.
Follow below code for better understanding
CODE
def add(a, b):
print "ADDING %d + %d" % (a, b)
return a + b
def subtract(a, b):
print "SUBTRACTING %d - %d" % (a, b)
return a - b
def multiply(a, b):
print "MULTIPLYING %d * %d" % (a, b)
return a * b
def divide(a, b):
print "DIVIDING %d / %d" % (a, b)
return a / b
print "Let's do some math with just functions!"
age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)
print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
# A puzzle for the extra credit, type it in anyway.
print "Here is a puzzle."
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print "That becomes: ", what, "Can you do it by hand?"
We are now doing our own math functions for add, subtract, multiply, and divide. The important thing to notice is the last line where we say return a + b (in add). What this does is the following:
Our function is called with two arguments: a and b.
We print out what our function is doing, in this case "ADDING."
Then we tell Python to do something kind of backward: we return the addition of a + b. You might say this as, "I add a and b then return them."
Python adds the two numbers. Then when the function ends, any line that runs it will be able to assign this a + b result to a variable.
The simple truth is that print and return have nothing to do with each other. print is used to display things in the terminal (for command-line programs).1 return is used to get a result back when you call a function, so that you can use it in the next step of the program's logic.
Many beginners are confused when they try out code at Python's interpreter prompt2, like
>>> def example():
... return 1
...
>>> example()
1
The value was displayed; doesn't this mean that return displays things? No. If you try the same code in a .py file, you can see for yourself that running the script doesn't cause the 1 to display.
This shouldn't actually be confusing, because it works the same way as any other expression:
>>> 1 + 1
2
This displays at the interactive prompt, but not if we make a script that just says 1 + 1 and try running it.
Again: if you need something to display as part of your script, print it. If you need to use it in the next step of the calculation, return it.
The secret is that the interactive prompt is causing the result to be displayed, not the code. It's a separate step that the prompt does for you, so that you can see how the code works a step at a time, for testing purposes.
Now, let's see what happens with print:
>>> def example():
... return 'test'
...
>>> print(example())
test
The result will display, whether we have this in an interactive prompt or in a script. print is explicitly used to display the value - and as we can see, it displays differently. The interactive prompt uses what is called the repr of the value that was returned from example, while print uses the str of the value.
In practical terms: print shows us what the value looks like, in text form (for a string, that just means the contents of the string as-is). The interactive prompt shows us what the value is - typically, by writing something that looks like the source code we would use to create it.3
But wait - print is a function, right? (In 3.x, anyway). So it returned a value, right? Isn't the interpreter prompt supposed to display that in its separate step? What happened?
There is one more trick: print returns the special value None, which the interpreter prompt will ignore. We can test this by using some expressions that evaluate to None:
>>> None
>>> [None][0]
>>> def example():
... pass # see footnote 4
...
>>> example()
>>>
In each case, there is no separate line at all for output, not even a blank line - the interpreter prompt just goes back to the prompt.
1 It can also be used to write into files, although this is a less common idea and normally it will be clearer to use the .write method.
2 This is sometimes called the REPL, which stands for "read-eval-print loop".
3 This isn't always practical, or even possible - especially once we start defining our own classes. The firm rule is that repr will lean on the .__repr__ method of the object to do the dirty work; similarly, str leans on .__str__.
4 Functions in Python implicitly return None if they don't explicitly return a value.
Return statement -- will return some values according your function.
def example(n):
if n == 5:
return true
else:
return false
if you call above function and you pass number 5 then it will return true else it will return false.
Printing function -- it will print content that you have given to the print function or with in print function bracket.
def example(n):
if n == 5:
print("number is equal")
else:
print("number is not equal")

Identifying and handling more than one dataframe in Python [duplicate]

Suppose I have code like:
x = 0
y = 1
z = 2
my_list = [x, y, z]
for item in my_list:
print("handling object ", name(item)) # <--- what would go instead of `name`?
How can I get the name of each object in Python? That is to say: what could I write instead of name in this code, so that the loop will show handling object x and then handling object y and handling object z?
In my actual code, I have a dict of functions that I will call later after looking them up with user input:
def fun1():
pass
def fun2():
pass
def fun3():
pass
fun_dict = {'fun1': fun1,
'fun2': fun2,
'fun3': fun3}
# suppose that we get the name 'fun3' from the user
fun_dict['fun3']()
How can I create fun_dict automatically, without writing the names of the functions twice? I would like to be able to write something like
fun_list = [fun1, fun2, fun3] # and I'll add more as the need arises
fun_dict = {}
for t in fun_list:
fun_dict[name(t)] = t
to avoid duplicating the names.
Objects do not necessarily have names in Python, so you can't get the name.
When you create a variable, like the x, y, z above then those names just act as "pointers" or "references" to the objects. The object itself does not know what name(s) you are using for it, and you can not easily (if at all) get the names of all references to that object.
However, it's not unusual for objects to have a __name__ attribute. Functions do have a __name__ (unless they are lambdas), so we can build fun_dict by doing e.g.
fun_dict = {t.__name__: t for t in fun_list)
That's not really possible, as there could be multiple variables that have the same value, or a value might have no variable, or a value might have the same value as a variable only by chance.
If you really want to do that, you can use
def variable_for_value(value):
for n,v in globals().items():
if v == value:
return n
return None
However, it would be better if you would iterate over names in the first place:
my_list = ["x", "y", "z"] # x, y, z have been previously defined
for name in my_list:
print "handling variable ", name
bla = globals()[name]
# do something to bla
This one-liner works, for all types of objects, as long as they are in globals() dict, which they should be:
def name_of_global_obj(xx):
return [objname for objname, oid in globals().items()
if id(oid)==id(xx)][0]
or, equivalently:
def name_of_global_obj(xx):
for objname, oid in globals().items():
if oid is xx:
return objname
As others have mentioned, this is a really tricky question. Solutions to this are not "one size fits all", not even remotely. The difficulty (or ease) is really going to depend on your situation.
I have come to this problem on several occasions, but most recently while creating a debugging function. I wanted the function to take some unknown objects as arguments and print their declared names and contents. Getting the contents is easy of course, but the declared name is another story.
What follows is some of what I have come up with.
Return function name
Determining the name of a function is really easy as it has the __name__ attribute containing the function's declared name.
name_of_function = lambda x : x.__name__
def name_of_function(arg):
try:
return arg.__name__
except AttributeError:
pass`
Just as an example, if you create the function def test_function(): pass, then copy_function = test_function, then name_of_function(copy_function), it will return test_function.
Return first matching object name
Check whether the object has a __name__ attribute and return it if so (declared functions only). Note that you may remove this test as the name will still be in globals().
Compare the value of arg with the values of items in globals() and return the name of the first match. Note that I am filtering out names starting with '_'.
The result will consist of the name of the first matching object otherwise None.
def name_of_object(arg):
# check __name__ attribute (functions)
try:
return arg.__name__
except AttributeError:
pass
for name, value in globals().items():
if value is arg and not name.startswith('_'):
return name
Return all matching object names
Compare the value of arg with the values of items in globals() and store names in a list. Note that I am filtering out names starting with '_'.
The result will consist of a list (for multiple matches), a string (for a single match), otherwise None. Of course you should adjust this behavior as needed.
def names_of_object(arg):
results = [n for n, v in globals().items() if v is arg and not n.startswith('_')]
return results[0] if len(results) is 1 else results if results else None
If you are looking to get the names of functions or lambdas or other function-like objects that are defined in the interpreter, you can use dill.source.getname from dill. It pretty much looks for the __name__ method, but in certain cases it knows other magic for how to find the name... or a name for the object. I don't want to get into an argument about finding the one true name for a python object, whatever that means.
>>> from dill.source import getname
>>>
>>> def add(x,y):
... return x+y
...
>>> squared = lambda x:x**2
>>>
>>> print getname(add)
'add'
>>> print getname(squared)
'squared'
>>>
>>> class Foo(object):
... def bar(self, x):
... return x*x+x
...
>>> f = Foo()
>>>
>>> print getname(f.bar)
'bar'
>>>
>>> woohoo = squared
>>> plus = add
>>> getname(woohoo)
'squared'
>>> getname(plus)
'add'
Use a reverse dict.
fun_dict = {'fun1': fun1,
'fun2': fun2,
'fun3': fun3}
r_dict = dict(zip(fun_dict.values(), fun_dict.keys()))
The reverse dict will map each function reference to the exact name you gave it in fun_dict, which may or may not be the name you used when you defined the function. And, this technique generalizes to other objects, including integers.
For extra fun and insanity, you can store the forward and reverse values in the same dict. I wouldn't do that if you were mapping strings to strings, but if you are doing something like function references and strings, it's not too crazy.
Note that while, as noted, objects in general do not and cannot know what variables are bound to them, functions defined with def do have names in the __name__ attribute (the name used in def). Also if the functions are defined in the same module (as in your example) then globals() will contain a superset of the dictionary you want.
def fun1:
pass
def fun2:
pass
def fun3:
pass
fun_dict = {}
for f in [fun1, fun2, fun3]:
fun_dict[f.__name__] = f
Here's another way to think about it. Suppose there were a name() function that returned the name of its argument. Given the following code:
def f(a):
return a
b = "x"
c = b
d = f(c)
e = [f(b), f(c), f(d)]
What should name(e[2]) return, and why?
And the reason I want to have the name of the function is because I want to create fun_dict without writing the names of the functions twice, since that seems like a good way to create bugs.
For this purpose you have a wonderful getattr function, that allows you to get an object by known name. So you could do for example:
funcs.py:
def func1(): pass
def func2(): pass
main.py:
import funcs
option = command_line_option()
getattr(funcs, option)()
I know This is late answer.
To get func name , you can use func.__name__
To get the name of any python object that has no name or __name__ method. You can iterate over its module members.
Ex:.
# package.module1.py
obj = MyClass()
# package.module2.py
import importlib
def get_obj_name(obj):
mod = Obj.__module__ # This is necessary to
module = module = importlib.import_module(mod)
for name, o in module.__dict__.items():
if o == obj:
return name
Performance note: don't use it in large modules.
Variable names can be found in the globals() and locals() dicts. But they won't give you what you're looking for above. "bla" will contain the value of each item of my_list, not the variable.
Generally when you are wanting to do something like this, you create a class to hold all of these functions and name them with some clear prefix cmd_ or the like. You then take the string from the command, and try to get that attribute from the class with the cmd_ prefixed to it. Now you only need to add a new function/method to the class, and it's available to your callers. And you can use the doc strings for automatically creating the help text.
As described in other answers, you may be able to do the same approach with globals() and regular functions in your module to more closely match what you asked for.
Something like this:
class Tasks:
def cmd_doit(self):
# do it here
func_name = parse_commandline()
try:
func = getattr('cmd_' + func_name, Tasks())
except AttributeError:
# bad command: exit or whatever
func()
I ran into this page while wondering the same question.
As others have noted, it's simple enough to just grab the __name__ attribute from a function in order to determine the name of the function. It's marginally trickier with objects that don't have a sane way to determine __name__, i.e. base/primitive objects like basestring instances, ints, longs, etc.
Long story short, you could probably use the inspect module to make an educated guess about which one it is, but you would have to probably know what frame you're working in/traverse down the stack to find the right one. But I'd hate to imagine how much fun this would be trying to deal with eval/exec'ed code.
% python2 whats_my_name_again.py
needle => ''b''
['a', 'b']
[]
needle => '<function foo at 0x289d08ec>'
['c']
['foo']
needle => '<function bar at 0x289d0bfc>'
['f', 'bar']
[]
needle => '<__main__.a_class instance at 0x289d3aac>'
['e', 'd']
[]
needle => '<function bar at 0x289d0bfc>'
['f', 'bar']
[]
%
whats_my_name_again.py:
#!/usr/bin/env python
import inspect
class a_class:
def __init__(self):
pass
def foo():
def bar():
pass
a = 'b'
b = 'b'
c = foo
d = a_class()
e = d
f = bar
#print('globals', inspect.stack()[0][0].f_globals)
#print('locals', inspect.stack()[0][0].f_locals)
assert(inspect.stack()[0][0].f_globals == globals())
assert(inspect.stack()[0][0].f_locals == locals())
in_a_haystack = lambda: value == needle and key != 'needle'
for needle in (a, foo, bar, d, f, ):
print("needle => '%r'" % (needle, ))
print([key for key, value in locals().iteritems() if in_a_haystack()])
print([key for key, value in globals().iteritems() if in_a_haystack()])
foo()
You define a class and add the Unicode private function insert the class like
class example:
def __init__(self, name):
self.name = name
def __unicode__(self):
return self.name
Of course you have to add extra variable self.name which is the name of the object.
Here is my answer, I am also using globals().items()
def get_name_of_obj(obj, except_word = ""):
for name, item in globals().items():
if item == obj and name != except_word:
return name
I added except_word because I want to filter off some word used in for loop.
If you didn't add it, the keyword in for loop may confuse this function, sometimes the keyword like "each_item" in the following case may show in the function's result, depends on what you have done to your loop.
eg.
for each_item in [objA, objB, objC]:
get_name_of_obj(obj, "each_item")
eg.
>>> objA = [1, 2, 3]
>>> objB = ('a', {'b':'thi is B'}, 'c')
>>> for each_item in [objA, objB]:
... get_name_of_obj(each_item)
...
'objA'
'objB'
>>>
>>>
>>> for each_item in [objA, objB]:
... get_name_of_obj(each_item)
...
'objA'
'objB'
>>>
>>>
>>> objC = [{'a1':'a2'}]
>>>
>>> for item in [objA, objB, objC]:
... get_name_of_obj(item)
...
'objA'
'item' <<<<<<<<<< --------- this is no good
'item'
>>> for item in [objA, objB]:
... get_name_of_obj(item)
...
'objA'
'item' <<<<<<<<--------this is no good
>>>
>>> for item in [objA, objB, objC]:
... get_name_of_obj(item, "item")
...
'objA'
'objB' <<<<<<<<<<--------- now it's ok
'objC'
>>>
Hope this can help.
Based on what it looks like you're trying to do you could use this approach.
In your case, your functions would all live in the module foo. Then you could:
import foo
func_name = parse_commandline()
method_to_call = getattr(foo, func_name)
result = method_to_call()
Or more succinctly:
import foo
result = getattr(foo, parse_commandline())()
Python has names which are mapped to objects in a hashmap called a namespace. At any instant in time, a name always refers to exactly one object, but a single object can be referred to by any arbitrary number of names. Given a name, it is very efficient for the hashmap to look up the single object which that name refers to. However given an object, which as mentioned can be referred to by multiple names, there is no efficient way to look up the names which refer to it. What you have to do is iterate through all the names in the namespace and check each one individually and see if it maps to your given object. This can easily be done with a list comprehension:
[k for k,v in locals().items() if v is myobj]
This will evaluate to a list of strings containing the names of all local "variables" which are currently mapped to the object myobj.
>>> a = 1
>>> this_is_also_a = a
>>> this_is_a = a
>>> b = "ligma"
>>> c = [2,3, 534]
>>> [k for k,v in locals().items() if v is a]
['a', 'this_is_also_a', 'this_is_a']
Of course locals() can be substituted with any dict that you want to search for names that point to a given object. Obviously this search can be slow for very large namespaces because they must be traversed in their entirety.
Hi there is one way to get the variable name that stores an instance of a class
is to use
locals()
function, it returns a dictionary that contains the variable name as a string and its value

How to overload method for showing the element in another class [duplicate]

In Java, if I call List.toString(), it will automatically call the toString() method on each object inside the List. For example, if my list contains objects o1, o2, and o3, list.toString() would look something like this:
"[" + o1.toString() + ", " + o2.toString() + ", " + o3.toString() + "]"
Is there a way to get similar behavior in Python? I implemented a __str__() method in my class, but when I print out a list of objects, using:
print 'my list is %s'%(list)
it looks something like this:
[<__main__.cell instance at 0x2a955e95f0>, <__main__.cell instance at 0x2a955e9638>, <__main__.cell instance at 0x2a955e9680>]
how can I get python to call my __str__() automatically for each element inside the list (or dict for that matter)?
Calling string on a python list calls the __repr__ method on each element inside. For some items, __str__ and __repr__ are the same. If you want that behavior, do:
def __str__(self):
...
def __repr__(self):
return self.__str__()
You can use a list comprehension to generate a new list with each item str()'d automatically:
print([str(item) for item in mylist])
Two easy things you can do, use the map function or use a comprehension.
But that gets you a list of strings, not a string. So you also have to join the strings together.
s= ",".join( map( str, myList ) )
or
s= ",".join( [ str(element) for element in myList ] )
Then you can print this composite string object.
print 'my list is %s'%( s )
Depending on what you want to use that output for, perhaps __repr__ might be more appropriate:
import unittest
class A(object):
def __init__(self, val):
self.val = val
def __repr__(self):
return repr(self.val)
class Test(unittest.TestCase):
def testMain(self):
l = [A('a'), A('b')]
self.assertEqual(repr(l), "['a', 'b']")
if __name__ == '__main__':
unittest.main()
I agree with the previous answer about using list comprehensions to do this, but you could certainly hide that behind a function, if that's what floats your boat.
def is_list(value):
if type(value) in (list, tuple): return True
return False
def list_str(value):
if not is_list(value): return str(value)
return [list_str(v) for v in value]
Just for fun, I made list_str() recursively str() everything contained in the list.
Something like this?
a = [1, 2 ,3]
[str(x) for x in a]
# ['1', '2', '3']
This should suffice.
When printing lists as well as other container classes, the contained elements will be printed using __repr__, because __repr__ is meant to be used for internal object representation.
If we call: help(object.__repr__) it will tell us:
Help on wrapper_descriptor:
__repr__(self, /)
Return repr(self).
And if we call help(repr) it will output:
Help on built-in function repr in module builtins:
repr(obj, /)
Return the canonical string representation of the object.
For many object types, including most builtins, eval(repr(obj)) == obj.
If __str__ is implemented for an object and __repr__ is not repr(obj) will output the default output, just like print(obj) when non of these are implemented.
So the only way is to implement __repr__ for your class. One possible way to do that is this:
class C:
def __str__(self):
return str(f"{self.__class__.__name__} class str ")
C.__repr__=C.__str__
ci = C()
print(ci) #C class str
print(str(ci)) #C class str
print(repr(ci)) #C class str
The output you're getting is just the object's module name, class name, and then the memory address in hexadecimal as the the __repr__ function is not overridden.
__str__ is used for the string representation of an object when using print. But since you are printing a list of objects, and not iterating over the list to call the str method for each item it prints out the objects representation.
To have the __str__ function invoked you'd need to do something like this:
'my list is %s' % [str(x) for x in myList]
If you override the __repr__ function you can use the print method like you were before:
class cell:
def __init__(self, id):
self.id = id
def __str__(self):
return str(self.id) # Or whatever
def __repr__(self):
return str(self) # function invoked when you try and print the whole list.
myList = [cell(1), cell(2), cell(3)]
'my list is %s' % myList
Then you'll get "my list is [1, 2, 3]" as your output.

Why are changes to a list, in an instance of a python 3 class, global?

Why when I create a class with a list, the list and its contents become global
class A:
my_list = []
string = ""
def add(self, data):
self.string += "a"
self.my_list.append(data)
def print_list(self):
print(self.string)
print(self.my_list)
a = A()
b = A()
a.add("test")
a.print_list()
b.print_list()
Both a and b will print the list that was created by a.add
# results of a.print_list
xa
['test']
# results of b.print_list
x
['test']
So my question is, is this normal for python3, or a bug.
Doesnt seem right to me that only the list is modified globally.
This is the explaining:
Objects have individuality, and multiple names (in multiple scopes) can be bound to the same object. This is known as aliasing in other languages. This is usually not appreciated on a first glance at Python, and can be safely ignored when dealing with immutable basic types (numbers, strings, tuples). However, aliasing has a possibly surprising effect on the semantics of Python code involving mutable objects such as lists, dictionaries, and most other types. This is usually used to the benefit of the program, since aliases behave like pointers in some respects. For example, passing an object is cheap since only a pointer is passed by the implementation; and if a function modifies an object passed as an argument, the caller will see the change — this eliminates the need for two different argument passing mechanisms as in Pascal.
From oficial Python 3 docs
And this is the solution:
(...) use an instance variable instead (...)
Like this:
class Dog:
def __init__(self, name):
self.name = name
self.tricks = [] # creates a new empty list for each dog
def add_trick(self, trick):
self.tricks.append(trick)
Result of above code:
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks ['roll over']
>>> e.tricks ['play dead']
In your example, you should move your my_list = [] declaration to the init function...
class A:
string = ""
def __init__(self):
self.my_list = []
def add(self, data):
self.string += "a"
self.my_list.append(data)
def print_list(self):
print(self.string)
print(self.my_list)
a = A()
b = A()
a.add("test")
a.print_list()
b.print_list()
Hope this help.
Regards
Strings are immutable so
self.string += "a"
creates a new object and binds it to self.string
This is clearly mutating the list in place
self.my_list.append(data)
Perhaps more interesting is that
self.my_list += [data]
also mutates the list
The general rule is that __iadd__ does behave differently for mutable vs immutable objects

How to create a list from user input in python then output list details

Pseudo-Code
def main():
create an empty list
value = getInput()
while value isnt zero:
add value to the list
value = getInput()
printOutput(list)
def getInput():
prompt the user for a value
make sure that the value is an int (convert to int)
return the number
def printOutput(list):
print out the number of input values
print out the individual input values
print out the sum of the input values
Your problem is that when you define and call printOutput, you name the argument list. However, in both main() and printOutput, the variable being used is actually named num. What happens is this:
main calls printOutput(list), passing it the built-in list:
printOutput receives that argument and assigns it to the local name list; and
The first line of printOutput tries to use num, which is not in scope, causing NameError.
You should edit the call and definition of printOutput to use num not list (indeed, you should never name variables that shadow built-ins like list, int, etc.). I suspect he argument is called list in the pseudo code to show the type of object the function should expect.
I believe you're looking for something like this:
def getInput():
try:
value=int(input("Enter a value: "))
except ValueError:
value=int(input("You must enter a number: "))
return value
def printOutput(lis):
print(len(lis))
print(lis)
print(sum(lis))
def main():
list_1=[]
value=getInput()
while value!=0:
list_1+=[value]
value=getInput()
printOutput(list_1)
main()

Resources