Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set and call object instance in Python

Not sure what I am doing wrong here when trying to initiate instance of object and set a name to the instance…

class Person:
    def Person(self, name):
        def __init__(self, name):
            self.name = name

m = Person.Person('James')

m.name

Any help with an explanation?

I’ve personally not encountered a situation where the init function is nested beneath a parent function…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem here is correlated to the function definition. Basically, you are calling Person function that define but doesn’t call the init one. So, you can solve like that:

class Person:
    def __init__(self, name):
        self.name = name
    
m = Person('James')
m.name
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading