Python defining function under condition - python

Is this code correct in python?
def foo(flag):
if flag:
def bar():
# Somthing
else:
def bar():
# Somthing else
bar()
foo(True)
foo(False)
if not what is a recommended way to set behavior of some function (bar) under? condition?
OK The real code is following
# Building replaceFunc based of ignore_case and use_regexp flags
if not ignore_case:
if not use_regexp:
def replaceFunc(string, search, replace):
return string.replace(search, replace)
else:
def replaceFunc(string, search, replace):
pattern = re.compile(search)
return pattern.sub(replace, string)
else:
if not use_regexp:
# There is no standard puthon function for replacing string by ignoring case
def replaceFunc(string, search, replace):
# implementation from http://stackoverflow.com/questions/919056/case-insensitive-replace
return string
else:
def replaceFunc(string, search, replace):
pattern = re.compile(search, re.IGNORECASE)
return pattern.sub(replace, string

Here's one reasonable way to achieve what you want:
def bar1():
return 'b1'
def bar2():
return 'b2'
def foo(flag):
bar = bar2 if flag else bar1
return bar()
print(foo(False))
print(foo(True))
One benefit of defining the functions bar1() and bar2() outside of foo() is that they can be unit tested.

Related

how to return the class function in python?

I called the other function from the first function.
when I return the first function, I am getting python None on the screen.
class test():
def test1(self, userinput):
return self.test2(userinput)
def test2(self, urls):
string = "this is " + urls
self.test3(string)
def test3(self, sentence):
return sentence
if __name__ == "__main__":
objectt = test()
seek = "concept of my functions"
print(objectt.test1(seek))
In test2, you call test3.
test3 returns 'this is concept of my function' to test2. Then, test2 returns nothing to test1 - so, as all functions that don't return anything specific, it returns None, which in turn is returned by test1.
You have to return the output of test3 at the end test2: class test():
class test:
def test1(self, userinput):
return self.test2(userinput)
def test2(self, urls):
string = "this is " + urls
return self.test3(string)
def test3(self, sentence):
return sentence
objectt = test()
seek = "concept of my functions"
print(objectt.test1(seek))
# this is concept of my functions

Is there any way to execute a statement before each return statement in python function?

For example i have this piece of code:
def example():
a = 'goodbye'
if True:
print a
return 1
else:
print a
return 0
I would like to know if there is any possible solution to write once "print a" and execute it before each "return" statement automaticaly. So that if I add more return statements I wouldn't need to add anything, but "print a" would execute. Result would look like something:
def example():
a = "goodbye"
""" some code to implement print a """
if True:
return 1
else:
return 0
Each time there is return statement it still would print a.
I tried to google, but don't know how word query, since all results are about returning multiple values.
UPDATE: My question was answered, thanks to all of you.
Although wrapping functions are correct answer, but I have chosen answer by GingerPlusPlus who suggested to use try...finally for simplicity.
try .. finally:
def example():
try:
if True:
return 1
else:
return 0
finally:
print 'goodbye'
>>> example()
goodbye
1
A finally clause is always executed before leaving the try statement, whether an exception has occurred or not. Docs
You can use a context. Initialize it with the value you want to print. Then print when context exit, i.e. upon return.
class PrinterOnContextExit():
def __init__( self, a ): self.a = a
def __enter__( self ): pass
def __exit__( self, exc_type, exc_value, traceback ): print( self.a )
def example():
a = 'goodbye'
with PrinterOnContextExit( a ):
if True:
return 1
else:
return 0
Note that you cannot print the returned value this way. If you ever wanted to print the returned value, then you should use a decorator.
class PrintOnReturn():
def __init__( self, a ): self.a = a
def __call__( self, func ): return lambda *args, **kwargs: self.callFunc( func, *args, **kwargs )
def callFunc( self, func, *args, **kwargs ): r = func( *args, **kwargs ); print( self.a, r ); return r
#PrintOnReturn( "hello" )
def example():
if True:
return 1
else:
return 0
This will print whatever string you passed to the decorator, followed by the value returned from the decorated function. Here hello 1.
Code:
def example():
a = 'goodbye'
print a
if True:
return 1
else:
return 0
If you print a before if else then it will print value every time you call the function.
You could also use a decorator, if it suits your case:
>>> def decorator(text):
... def wrapped(func):
... def inner(*args, **kwargs):
... result = func(*args, **kwargs)
... print text
... return result
... return inner
... return wrapped
...
>>> #decorator('goodbye')
... def example():
... return True
...
>>> example()
goodbye
>>> True
Decorator will allow you to print any text after the decorated function was called. Or before.
Create a value returnval
returnval = 0 #default value
testval = 0 # Code to set up if
# code to set various values of testval
if testval == 0:
returnval = 1
elif testval == 5:
returnval = 2
else:
returnval = 10
print a
return returnval
Def example():
a = 'goodbye'
if True:
return 1,str(a)
else:
return 0,str(a)
print example()
Thats the only way...I dont think there is a way to avoid typing what you want to be printed...sorry mate! expect if you type a function type the thinks you
An easier alternative as i've also posted here for a similar topic:
def master_example():
a = []
def example():
a.append('goodbye')
if True:
return 1
else:
return 0
example()
print a[0]

Iterator example from Dive Into Python 3

I'm learning Python as my 1st language from http://www.diveintopython3.net/. On Chp 7, http://www.diveintopython3.net/iterators.html, there is an example of how to use an iterator.
import re
def build_match_and_apply_functions(pattern, search, replace):
def matches_rule(word):
return re.search(pattern, word)
def apply_rule(word):
return re.sub(search, replace, word)
return [matches_rule, apply_rule]
class LazyRules:
rules_filename = 'plural6-rules.txt'
def __init__(self):
self.pattern_file = open(self.rules_filename, encoding='utf-8')
self.cache = []
def __iter__(self):
self.cache_index = 0
return self
def __next__(self):
self.cache_index += 1
if len(self.cache) >= self.cache_index:
return self.cache[self.cache_index - 1]
if self.pattern_file.closed:
raise StopIteration
line = self.pattern_file.readline()
if not line:
self.pattern_file.close()
raise StopIteration
pattern, search, replace = line.split(None, 3)
funcs = build_match_and_apply_functions(
pattern, search, replace)
self.cache.append(funcs)
return funcs
rules = LazyRules()
def plural(noun):
for matches_rule, apply_rule in rules:
if matches_rule(noun):
return apply_rule(noun)
if __name__ == '__main__':
import sys
if sys.argv[1:]:
print(plural(sys.argv[1]))
else:
print(__doc__)
My question is: how does the 'for matches_rule, apply_rule in rules:' loop in the plural(noun) function know when to exit after fulfilling the if condition? There are no StopIteration commands for that condition. I would expect the for loop to continue until the rules.cache is iterated completely.
Thank you for the help!
The return statement ends the function at that point, returning a value to the caller. This can be relied upon in almost any situation (if you have a try..except..else..finally structure, even a return statement won't prevent the finally block from being executed).

Python: Using the same function as both a generator and a regular function

Is it possible to use a function as both a regular function (that does stuff and returns) and a generator function?
Suppose I have the following code:
def __init__(self):
self.make_generator = False
def foo(self):
gen = (x for x in my_list)
for x in gen:
# Do some stuff
if self.make_generator:
yield
# This return will raise a StopIteration
return
The idea here was that if self.make_generator is False, then I don't want foo to yield; I want it to not be a generator so that I can call it like:
foo()
And if I wanted it to yield between iterations, I'd make it a generator like so:
def __init__(self):
self.make_generator = True
self.foo_gen = self.foo()
def run(self):
while True:
self.foo_gen.next()
But it seems that this is not possible. I want to keep my code dry but am not sure how I can control when foo is a generator and when it executes like a regular function.
Thanks!

Counting python method calls within another method

I'm actually trying doing this in Java, but I'm in the process of teaching myself python and it made me wonder if there was an easy/clever way to do this with wrappers or something.
I want to know how many times a specific method was called inside another method. For example:
def foo(z):
#do something
return result
def bar(x,y):
#complicated algorithm/logic involving foo
return foobar
So for each call to bar with various parameters, I'd like to know how many times foo was called, perhaps with output like this:
>>> print bar('xyz',3)
foo was called 15 times
[results here]
>>> print bar('stuv',6)
foo was called 23 times
[other results here]
edit: I realize I could just slap a counter inside bar and dump it when I return, but it would be cool if there was some magic you could do with wrappers to accomplish the same thing. It would also mean I could reuse the same wrappers somewhere else without having to modify any code inside the method.
Sounds like almost the textbook example for decorators!
def counted(fn):
def wrapper(*args, **kwargs):
wrapper.called += 1
return fn(*args, **kwargs)
wrapper.called = 0
wrapper.__name__ = fn.__name__
return wrapper
#counted
def foo():
return
>>> foo()
>>> foo.called
1
You could even use another decorator to automate the recording of how many times a function is called inside another function:
def counting(other):
def decorator(fn):
def wrapper(*args, **kwargs):
other.called = 0
try:
return fn(*args, **kwargs)
finally:
print '%s was called %i times' % (other.__name__, other.called)
wrapper.__name__ = fn.__name__
return wrapper
return decorator
#counting(foo)
def bar():
foo()
foo()
>>> bar()
foo was called 2 times
If foo or bar can end up calling themselves, though, you'd need a more complicated solution involving stacks to cope with the recursion. Then you're heading towards a full-on profiler...
Possibly this wrapped decorator stuff, which tends to be used for magic, isn't the ideal place to be looking if you're still ‘teaching yourself Python’!
This defines a decorator to do it:
def count_calls(fn):
def _counting(*args, **kwargs):
_counting.calls += 1
return fn(*args, **kwargs)
_counting.calls = 0
return _counting
#count_calls
def foo(x):
return x
def bar(y):
foo(y)
foo(y)
bar(1)
print foo.calls
After your response - here's a way with a decorator factory...
import inspect
def make_decorators():
# Mutable shared storage...
caller_L = []
callee_L = []
called_count = [0]
def caller_decorator(caller):
caller_L.append(caller)
def counting_caller(*args, **kwargs):
# Returning result here separate from the count report in case
# the result needs to be used...
result = caller(*args, **kwargs)
print callee_L[0].__name__, \
'was called', called_count[0], 'times'
called_count[0] = 0
return result
return counting_caller
def callee_decorator(callee):
callee_L.append(callee)
def counting_callee(*args, **kwargs):
# Next two lines are an alternative to
# sys._getframe(1).f_code.co_name mentioned by Ned...
current_frame = inspect.currentframe()
caller_name = inspect.getouterframes(current_frame)[1][3]
if caller_name == caller_L[0].__name__:
called_count[0] += 1
return callee(*args, **kwargs)
return counting_callee
return caller_decorator, callee_decorator
caller_decorator, callee_decorator = make_decorators()
#callee_decorator
def foo(z):
#do something
return ' foo result'
#caller_decorator
def bar(x,y):
# complicated algorithm/logic simulation...
for i in xrange(x+y):
foo(i)
foobar = 'some result other than the call count that you might use'
return foobar
bar(1,1)
bar(1,2)
bar(2,2)
And here's the output (tested with Python 2.5.2):
foo was called 2 times
foo was called 3 times
foo was called 4 times

Categories

Resources