| | |
calling a function without changing scope
Thread Solved
![]() |
•
•
Join Date: Dec 2006
Posts: 28
Reputation:
Solved Threads: 0
Hi,
My problem is that I have a large function with hundreds of lines. I want to break it apart into smaller functions. The issue is that if I make a module level function, the scope changes and I have to pass the necessary variables to it. This can be difficult if there are 10 variables that need to be passed. So basically I want to be able to call a function inside another function but keep the scope the same. I found a way to do something like this by defining a function inside another function:
The thing with this is that it doesn't really shorten my larger function. So is it possible to get the effect of the above code with insideFunc defined somewhere that is not inside of main?
My problem is that I have a large function with hundreds of lines. I want to break it apart into smaller functions. The issue is that if I make a module level function, the scope changes and I have to pass the necessary variables to it. This can be difficult if there are 10 variables that need to be passed. So basically I want to be able to call a function inside another function but keep the scope the same. I found a way to do something like this by defining a function inside another function:
python Syntax (Toggle Plain Text)
def main(): a = 'man' b = 'dog' def insideFunc(): print 'used local variable %s' % a print 'used local variable %s' % b insideFunc()
The thing with this is that it doesn't really shorten my larger function. So is it possible to get the effect of the above code with insideFunc defined somewhere that is not inside of main?
•
•
Join Date: Dec 2006
Posts: 977
Reputation:
Solved Threads: 273
You can use a class to do that in the following way. You could also use a list or dictionary as a container for the 10 variables and pass one list/dictionary instead of 10 variables. IMHO using the class method is preferred.
Python Syntax (Toggle Plain Text)
class test_class: def __init__(self): self.a="dog" self.b="cat" self.c=1 print "__init__, a, b, c =", self.a, self.b, self.c self.first_func() self.second_func("abc") def first_func(self): self.c += 1 print "first_func, a, b, c =", self.a, self.b, self.c def second_func(self, d): self.c += 1 print "second_func a, b, c =", self.a, self.b, self.c print "d =", d if __name__ == "__main__": TC=test_class() print "\n now calling the functions from outside the class" TC.first_func() TC.second_func("xyz")
Last edited by woooee; Feb 20th, 2008 at 4:18 pm.
![]() |
Similar Threads
- Send data on a serial port (C++)
- Some Code (C++)
- help please (C++)
Other Threads in the Python Forum
- Previous Thread: return value None
- Next Thread: repetitive functions
| Thread Tools | Search this Thread |
alarm ansi anydbm app assignment backend beginner binary bluetooth character cipher cmd coordinates customdialog cx-freeze data decimals development directory dynamic exe feet file float format function generator getvalue gnu graphics halp handling heads homework http ideas input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse number numbers output parsing path pointer prime programming progressbar push py2exe pygame pymailer python queue random recursion recursive schedule screensaverloopinactive script slicenotation sqlite ssh statistics string strings sudokusolver text thread time tlapse tuple ubuntu unicode url urllib urllib2 variable ventrilo vigenere web webservice wikipedia write wxpython xlib xlwt





