[Tutor] Simple Question On A Method (in subclass)

Alan Gauld alan.gauld at btinternet.com
Mon Oct 24 19:56:46 CEST 2011


On 24/10/11 11:17, lina wrote:
> <snip>
>
> a quick Q: Every time call the method, need go through the __initi__( ) part?

No.
__init__() is only called when an instance is first created.

Here is an example in the IDLE:

 >>> class C:
	def __init__(self):
		print("I'm in init")
	def method(self):
		print("I'm in method")

		
 >>> c = C()
I'm in init
 >>> c.method()
I'm in method
 >>> c2 = C().method()  # calls init for C(), then the method
I'm in init
I'm in method
 >>>

HTH,

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list