Is possible and, if so, common practice to initialize an object inside a class in Python?
self.field = Class()
Martijn Pieters
14.7k10 gold badges60 silver badges59 bronze badges
asked Nov 18, 2014 at 10:54
2 Answers 2
Everything in Python is an object, so yes, this is common practice. You could not create a meaningful Python program without creating other instances in your own classes.
answered Nov 18, 2014 at 11:01
-
Thank you Martinijn, I am new to Python and enjoying Oop !Codenburger– Codenburger2014年11月18日 11:37:50 +00:00Commented Nov 18, 2014 at 11:37
If you want to do it in a Object Oriented way, you could pass a factory into the class, so that you can instantiate your class using a factory method:
self.field = myFactory.GetSomeClass()
lang-py