Create an instance of a class in Python

We create instances of classes in python in the same way we create instances of any other types.  Let’s take an example class:

class Animal(object):

	def speak(self):
		print "I don’t know how to speak"
	
	def walk(self):
		print "I don’t know how to walk"
	
	def breath(self):
		print "I don’t know how to breath"

Now lets create a new instance of the animal class for 'tiger':

tiger = Animal()

Tiger = The variable name of our instance

Animal = The type of our instance

() = Adding brackets to indicate that we are creating a new instance

We can confirm that we are using the class by printing the type of our new tiger variable:

tiger = Animal()
print type(tiger)
Output: <class '__main__.Animal'>

Note: ‘__main__’ would be the script name, so for example if we were working on a script called ‘jungle.py’, the output of the above print command would be <class ‘jungle.Animal’>.

Lets say we now want to execute the speak method from within the Animal class, using our new tiger variable:

tiger = Animal()
tiger.speak()
Output: I don't know how to speak

We are evoking the speakmethod from the Animalclass using our tigerinstance.