1

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

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
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()
answered Nov 22, 2014 at 18:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.