1

Is possible and, if so, common practice to initialize an object inside a class in Python?

self.field = Class()

2 Answers 2

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.

1
  • Thank you Martinijn, I am new to Python and enjoying Oop ! Commented Nov 18, 2014 at 11:37
0

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.