-3

Use case is simple: I create an object from a class in Python. If I create that object outside of any function, I have no problem getting back any of the attributes.

But somehow if I happened to create that object inside a function, I cannot get it back anywhere.

This works:

class UserTest():
    def__init__(self,name,age,size):
        self.name=name
        self.age=age
        self.size=size

FirstUser=UserTest(name,age,size)

print(FirstUser.name,FirstUser.age,FirstUser.size)

But this does not:

class UserTest():
    def__init__(self,name,age,size):
        self.name=name
        self.age=age
        self.size=size

def createUser(name,age,size):
    FirstUser=Usertest(name,age,size)
    return FirstUser

createUser('Max',38,180)

print(FirstUser.name,FirstUser.age,FirstUser.size)

And I cannot figure why I cannot find the object I just created, even if I return the FirstUser object at the end of the function. What am I missing?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Akadok
  • 1

3 Answers3

0

Any variable assignment changes the scope of the variable to the current code block.

This means, in your code the variable FirstUser is only known inside createUser.

To use FirstUser as a global variable, you could use the global keyword:

def createUser(name,age,size):
    global FirstUser
    FirstUser=UserTest(name,age,size)
    return FirstUser

createUser('Max',38,180)

print(FirstUser.name,FirstUser.age,FirstUser.size)

or even better: do not use global variables, but work with the return value:

def createUser(name,age,size):
    return UserTest(name,age,size)

FirstUser = createUser('Max',38,180)

print(FirstUser.name,FirstUser.age,FirstUser.size)
Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
  • Thank you all. Apparently my question was immediately closed (which is too bad because I had a side question). Sorry, I did research, but I could not make any sense of it. I understand that you have to put a variable, even if temporary. But does that work the same way when you want ot update an existing attribute let's say INSIDE another function? – Akadok Apr 07 '21 at 12:58
  • @Akadok You might want to start here: https://stackoverflow.com/q/16959576/476. That's about PHP, not Python, but the basics are very similar, in that you need to understand *scope* first. Then it'll make more sense that you must pass *values* into functions as parameters and *return values* from functions. (Note that the scoping rules are slightly different between PHP and Python, so only take away the general idea, not the specifics.) – deceze Apr 07 '21 at 13:17
0

Object returned by createUser('Max',38,180) has to be assigned or cached into a reference variable which can then be used to call its members, like such
user = createUser('Max',38,180) then this reference variable user can be used to call its members.

Pawan Nirpal
  • 565
  • 1
  • 12
  • 29
0

The returned value from createUser is not assigned

You also have some typos : missing spaces and case

Input:

class UserTest():
    def __init__(self,name,age,size):
        self.name=name
        self.age=age
        self.size=size

def createUser(name,age,size):
    return UserTest(name,age,size)

## ANSWER CORE ##
firstUser = createUser('Max',38,180)

print(firstUser.name,firstUser.age,firstUser.size)

Output:

Max 38 180
hpchavaz
  • 1,368
  • 10
  • 16