I am just looking to understand why I get the following results
From the following code
def sendHTTP(httpStatus):
status_code = 400
reason = "Unauthorized"
httpStatus(Code=req.status_code,Desc=req.reason)
return httpStatus
if __name__ == '__main__':
httpStatus = namedtuple('httpStatus','Code Desc')
http_results = sendHTTP(httpStatus)
print "1:",http_results
Print result is:
1: <class '__main__.httpStatus'>
while
def sendHTTP(httpStatus):
status_code = 400
reason = "Unauthorized"
b = httpStatus(Code=req.status_code,Desc=req.reason)
return b
if __name__ == '__main__':
httpStatus = namedtuple('httpStatus','Code Desc')
http_results = sendHTTP(httpStatus)
print "1:",http_results
Print result is:
1: httpStatus(Code=401, Desc='Unauthorized')
Could someone explain why adding the b
variable gives the values instead of the variable name?
Because in the first case, you aren't doing anything with the resulting instance from the call of the httpStatus
, and then simply returning the namedtuple class which was passed as an argument.
Whereas in the second, b
is assigned the instance of httpStatus
which is created with
b = httpStatus(Code=req.status_code,Desc=req.reason)
which is then returned.
If you wanted the same (correct) behavior in both cases, you would want to directly
return httpStatus(Code=req.status_code,Desc=req.reason)
in your first case, instead of having the useless call without a return statement or assignment.