Python函数中是否存在用户不可访问参数的语法。甚至有可能吗?
例如,我想定义一个函数,该函数需要用户[
def function(userEntry, key1 = 0):
if key1 == 0: #setting 1
##SOME INITIAL OPERATIONS ##
key1 += 1
function(userEntry, key1)
if key1 == 1: #setting 2
##FINAL OPERATIONS##
print('Some function of ' userEntry)
如果按照上述方法进行操作,用户仍然可以根据需要访问key1并初始化程序,但是,我不希望用户能够执行此操作。我希望用户仅在函数需要根据用户输入和key1的条件调用自身时输入userEntry,操作将发生变化。
def userFunc(input):
# Do stuff
function(input, key)
def function
# Does common functionality
key = 0
def userFunc(input):
# Do stuff
function(input, key)
def function(userEntry, key1 = 0):
if key1 == 0: #setting 1
##SOME INITIAL OPERATIONS ##
print('initial operation')
print key1
key1 += 1
return ##to make the function not instantly repeat itself
if key1 == 1: #setting 2
##FINAL OPERATIONS##
print('final operation')
print key1
userFunc('inValue')
这将在首次调用userFunc()时执行初始操作,然后在下次调用它时执行最终操作。