What is the difference between void function and frutial function? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Now I am watching PY4E youtube, the difference between void and fruitial function does not touch me well. just return value?
Also, if the void function has a function PRINT(SOMETHING)
even though it has not return value, but it has some value or result?
isn't it? I am little bit confused.

In Python, all functions return something, but some return None, which is ignored. These are called "void". Functions that return anything else are called "fruitful".
For example, list.append():
>>> a = []
>>> a.append(1)
Note how nothing was printed after a.append(). But it did actually return None, which we can confirm by printing its return value:
>>> print(a.append(2))
None
print as well returns none:
>>> print(print(a.append(3)))
None
None
And user-defined functions which don't have a return statement return None.

Related

Function doesn't Break [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to break out or restart a function in python 2. Putting a return statement should stop all execution. Same goes for restarting the function.
def function():
...
if len(lst) == 1:
return value
print 'foo'
else:
function()
print 'foo'
In this case 'foo' would be printed twice.
The return statement only stops the execution of the current instance of the function.
Since you're doing a recursion, you will still get the other calls of that function running until they hit their own return (if any).

what are python closures good for? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I understand the technical definition of python closures: let's make it concrete.
def foo(x):
def bar(y):
print(x+y)
return bar
In this example x will get bound with bar. But what are these things actually good for? ie in the toy example above one could have just as easily written
def bar(x,y):
print(x+y)
I would like to know the best use cases for using closures instead of, for example, adding extra arguments to a function.
I think the most used example of closure is for caching functions with a decorator.
def cache_decorator(f):
cache = {}
def wrapper(*args):
if args not in cache:
cache[args] = f(*args)
return cache[args]
return wrapper
#cache_decorator
def some_function(*args):
...
This way the cache cannot be referenced from anywhere, since you do not want your users to tamper with it.

What's the correct way to pass by reference [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have seen other people ask a question but the only answers I have seen simply explain that python doesn't have the same concept of pass by reference vs pass by value as languages like C do.
for example
x=[0]
def foo(x):
x[0] += 1
In the past, I have been using this work around but it seems very un-pythonic so I'm wondering if there is a better way to do this.
let's assume for what ever reason returning values won't work, like in the case where this code runs on a separate thread.
Some python objects are immutable (tuple, int, float, str, etc). As you have noted, you cannot modify these in-place.
The best workaround is to not try to fake passing by reference, instead you should assign the result. This is both easier to read and less error prone.
In your case, you could call:
x = 0
def f(x):
return x + 1
x = f(x)
If you truly need to fake passing by reference (and I don't see why you would need that), your solution works just fine, but keep in mind that you do not actually modify the object.
x = 0
x_list = [x]
print(id(x_list[0])) # 1844716176
def f(x_list):
x_list[0] += 1
f(x_list)
print(x) # 0, not modified
print(id(x_list[0])) # 1844716208

Why is it bad to append dictionaries with x = dict(x, **y)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
The refutations in this thread make no sense to me whatsoever. Can someone break it down? Make analogies? Why is this an "abusive hack"?
dict(x, **foo)
...given foo = { 'hello': 'world' }, does the following:
dict(x, hello=world)
This is reasonably straightforward -- however, kwargs behavior is only well-defined for keys which actually could be passed as keyword arguments in (all available versions of) Python. Consider something like:
foo = { ('some', 'tuple'): 'value' }
...in which case you have a key which couldn't actually be passed as a keyword argument; to pass **foo would be to exercise behavior which is not intuitively defined to readers, and which some versions of Python (such as Python 3) will explicitly reject.
By contrast:
x = dict(x) # create a new object
x.update(y)
...is relying only on well-defined behavior, and is portable going forward.

Modify docstring in a decorator [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am using flask, and some functions have decorators, to check the presence of some headers and return some error codes if they are missing.
In these decorators, before returning the decorated function i do something like
decorated_function.__doc__ += "Returns 400 if the X-Version header is not present."
Is this pythonic? Is there a better way to achieve it?
I am using wraps already from functools.
def ModDoc(doc):
def wrapped(func):
func.__doc__ = doc
return func
return wrapped
#ModDoc("test2")
def test():
"""test"""
return
print test.__doc__
Will modify the docstring of anything it is applied too. Remember these changes are purely interactive, and will not show up in stored or auto generated documentation.

Categories

Resources