Why do python classes run their code when they are defined? [Conceptual]

Question

I apologize in advance for being a total newb to OOP (and stack overflow as well). I am trying to fully comprehend some of the concepts, despite ample reading on wikipedia/python docs/stack overflow/my trusty Zed Shaw book. Here is a question I could not find on google, perhaps because it is difficult to phrase.

I am testing this on the command line, but I assume that the same rules apply to in-script class definitions as well.

When I define this pointless class named "Class1"

>>> class Class1(): 
>>>     print "This class is being defined"

This class is being defined

The output is "This class is being defined". Although I did not desire any output here. Why did it run that internal code? Isn't that code inside a class that has not been instantiated?

Bonus Question: Am I correct to be using the word "Define" in "define a class"?


Show source
| class   | python   2017-01-07 20:01 0 Answers

Answers ( 0 )

◀ Go back