Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unusable function arguments

Is there a syntax for user-inaccessible arguments in Python functions. Or is that even possible?

For example, I would like to define a function that takes only a single argument from the user but there is a need for another argument where the function needs to call itself in a different setting, such as:

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) 

If done as above, user can still access key1 and initialize the program as they wish, however, I do not want user to be able to do this. I want the user to enter userEntry only while the function requires to call itself depending on the conditions on user-input and key1, operations will change.

like image 516
Semzem Avatar asked Jun 17 '26 01:06

Semzem


2 Answers

I agree that having a separate function that the user calls would be a good idea. I have made some code that works for that here:

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')

This would do the initial operations the first time userFunc() is called, then the final operations the next time it is called.

like image 80
Jelly Joe Avatar answered Jun 18 '26 16:06

Jelly Joe


Have a function that the end user calls, that does what it needs to, before calling the actual function which you use everywhere else.

def userFunc(input):
    # Do stuff
    function(input, key)

def function
    # Does common functionality
like image 44
Sayse Avatar answered Jun 18 '26 14:06

Sayse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!