hello, I would like to understand these two code in Python and what is the function of (self) in the set_name function

Asked

Viewed 22 times

0

hello, I’m new to Python and programming in general, I just got into object orientation and I can’t understand why 'self' in the following code functions; I think because I studied an introduction of POO in c++ this python code has become very complicated. if you can compare with c++ thank you.

class Person(object):
    pass

def set_name (self,name):
    self.name = name
    
woman = Person()

set_name(woman,'julia')


print (woman.name)

#########################################.

class Person(object):

    def set_name (person,name):
        person.name = name
    
woman = Person()


woman.set_name('julia')

print (woman.name)
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.