繁体   English   中英

如何在另一个 function 中使用来自一个 function 的本地定义变量?

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

我对 python (PYTHON 3.9.1) 还是新手,我正在尝试制作一个程序,它将获得 ip 地址,然后是 hash 地址。 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). 然后我定义了一个使用 SHA256 散列的 function。 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. 由于十六进制 IP 地址未全局定义,因此我无法在另一个 function 中使用它。 我可以将散列 function 的代码放入第一个 function 中,我做到了,它有效,但我不喜欢它,它使我的代码混乱,我认为必须有另一种方法来做到这一点.

主要问题是:如何在另一个 function 中使用来自一个 function 的本地定义变量?

这是我的代码:

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.

如您所见,get_ip() function 中有一个本地定义的变量 hexidecimal_ip。 我想在我的 sha256() function 中使用这个本地定义的变量,所以我所做的是:

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

但是当然它不起作用,因为变量不是全局定义的,所以我接下来要做的是:

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

然而这也不起作用,所以对我来说唯一剩下的方法就是把它全部放在一个 function 中,我做了并且它有效:

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

因此,在对我的问题进行了长时间介绍之后,我的问题仍然存在:如何在另一个 function 中使用来自一个 function 的本地定义变量?

有很多方法可以做到这一点:-

您可以在返回后将 hexidecimal_ip 作为参数传递给 sha256 function,例如:-

def get_ip():
    .
    .
    return hexidecimal_ip

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

在主要的 function 中,您可以执行以下操作:-

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

当然还有其他方法,但这可能是最好的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM