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

new to python and need to know how __init__ behave

i want to know why the output does not change my namef to "yolo" , and why it returns nonex2 when i remove the # from the first print statement

class Class():
    def __init__(self,namef,namel):
        self.name1=namef
        self.name2=namel
        self.email=print(f"{self.name1}{self.name2}@google.com")


    def introself(self):
        return(f"Hello guys my name is {self.name1}{self.name2} and i am a human")

human1=Class("chandra", "Shekar")

 # print (human1.email)

human1.name1="yolo"

print (human1.email)

>Solution :

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

By setting human1.name1="yolo" you have updated an attribute of the instance "human1" of the class "Class". Now if you print human1.name1 it’ll be "yolo" as expected. namef is a parameter, not a variable, not a value, so I don’t see how you could even see that it isn’t changed as it never existed. As for human1.email, you’ve set it as print("a string"), print is a built-in function, calling print like this prints the string, but returns none, and then you printed none, so it shows none.

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