How do i print out a object from __init__ to main()? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do i print out a object from __init__ to main()?

Class Cat: def __init__(self, catname): self.catname = catname def give_name(self): self.catname = "Tony" def return_catname(self): return self.catname def main(): cat_object = Cat(catname) print(cat_object.return_catname())

21st Apr 2020, 5:09 PM
W. Khalid
1 Answer
+ 4
1 class is lowercase 2 check your indentation levels 3 pass a valid string or variable to the constructor 4 call the main function to run it #How do i print out a object from __init__ to main()? class Cat: def __init__(self, catname): self.catname = catname def give_name(self): self.catname = "Tony" def return_catname(self): return self.catname def main(): cat_object = Cat("Kitty") print(cat_object.return_catname()) main() If you're going to use methods to access and modify your class members then you should change the access modifiers of those members. IE cat_object.catname is accessible.
21st Apr 2020, 5:41 PM
ChaoticDawg
ChaoticDawg - avatar