Search code examples
pythonhashlocal-variables

How do I use a locally defined variable from one function in another function?


I am still new to python (PYTHON 3.9.1), and I am trying to make a programm that will get an ip address and then hash it. I defined a function that gets ip address, then removes dots and converts it to hexidecimal (I just want it to hash the hex of ip address). Then I defined a function that hashes using SHA256. But now when I want to use the locally defined variable that defines the Hexed IP address in the first function, in the second function to hash it, I just dont know how to do it. As the Hexed IP address is not globally defined I can't use it in another function. I could just put the code of the hashing function into the first function, and I did it and it works, but I don't like it and it makes my code messy, and I think there has got to be another way to do it.

MAIN QUESTION IS: How do I use a locally defined variable from one function in another function ?

Here is my code:

import hashlib as hb
import socket

def get_ip():
    hostname = socket.gethostname()
    ip = socket.gethostbyname(hostname) #If I was to print it, it would give a propper IP address
    dotless_ip = ip.replace(".","") 
    hexidecimal_ip = hex(int(dotless_ip))

def sha256(): #This is the hashing function that uses SHA256 algorythm for hashing.
    SHA256_convert = bytearray(input("Enter text to convert to SHA256 hash: "), "utf8") #If I was to hash something with this function, I would need to input stuff myself.
    hasher = hb.sha256()
    hasher.update(SHA256_convert)
    print(hasher.hexdigest()) #This function works too, and the problem is not in it.

So as you can see the get_ip() function has a locally defined variable hexidecimal_ip in it. I want to use this locally defined variable in my sha256() function, so what I did is:

def sha256():
    SHA256_convert = bytearray(hexidecimal_ip), "utf8") #changing user input to a variable.
    hasher = hb.sha256()
    hasher.update(SHA256_convert)
    print(hasher.hexdigest()) 

But of course it didn't work because the variable is not globally defined, so the next thing I did is:

def get_ip():
    hostname = socket.gethostname()
    ip = socket.gethostbyname(hostname)
    dotless_ip = ip.replace(".","") 
    hexidecimal_ip = hex(int(dotless_ip))
    return hexidecimal_ip #added a return statement here, in hopes that it will make the variable accessible globaly ( I still don't understand how return works )

def sha256():
    SHA256_convert = bytearray(hexidecimal_ip), "utf8") #changing user input to a variable.
    hasher = hb.sha256()
    hasher.update(SHA256_convert)
    print(hasher.hexdigest()) 

However this did not work either, so the only left way to do it for me was to just put it all into one function which I did and it works:

def get_ip():
    hostname = socket.gethostname()
    ip = socket.gethostbyname(hostname)
    print("Your IP address is:",ip) #I added a few print statements to check if function works and it does
    dotless_ip = ip.replace(".","")
    hexidecimal_ip = hex(int(dotless_ip))
    print("Your IP address in hexidecimals is: ", hexidecimal_ip) 
    ip_hasher = hb.sha256() #changed the variable name a little bit, but it doesn't really matter
    ip_hasher.update(bytearray(hexidecimal_ip, "utf8"))
    print("Your hashed IP address is: ",ip_hasher.hexdigest())

So after this long introduction to my problem my question remains: How do I use a locally defined variable from one function in another function ?


Solution

  • There are many ways to do this:-

    You can pass the hexidecimal_ip as a parameter to sha256 function after returning it, for example:-

    def get_ip():
        .
        .
        return hexidecimal_ip
    
    def sha256(hexidecimal_ip):
        SHA256_convert = bytearray(hexidecimal_ip), "utf8")
        .
        .
    

    and in the main function you can do:-

    def main():
        hexidecimal_ip = get_ip()
        sha256(hexidecimal_ip)
    

    of course there are other ways also but this might be the best