Hanna Thomas Hanna Thomas - 7 months ago 11
Python Question

Recasting Python Variable

In python I have a variable set as a string which is a username:

self.loggedInUser = "Hanna"


When I have retrieved the user details as a list, I would like to reuse the variable:

self.loggedInUser = (0, "Hanna", "hash", "UID")


Is it possible to do this, probably the more important, is it bad practice?

H

Answer

Python lets you change the value of a variable at any time, so it's certainly possible to change the value of self.loggedInUser from a string to a list or tuple, or any other kind of object.

This can be confusing though, and could cause errors in your code if it's expecting one kind of object and finds a different kind, so it's better to split them up, say as self.loggedInUserName and self.loggedInUser.

Comments