What does instantiate mean in the context of this lesson?

I’m struggling to get my head around what the word “instantiate” means, in this context. Can someone try to break down what that term means? And perhaps explain classes a different way, because I’m finding this whole section pretty confusing…

For example, in this exercise it says, “The first dunder method we’re going to use is the __init__ method… This method is used to initialize a newly created object. It is called every time the class is instantiated.”

What does that mean?

It means the class is invoked to create a new instance, an object constructed of the attributes of the class, using the unique values that were passed to the __init__ method as instance variables.

class Foo:
    def __init__(self, bar):
        self.bar = bar

a = Foo('foo')
b = Foo('bar')

a and b above are both instances of the Foo class.

a.bar  =>  foo
b.bar  =>  bar

I was puzzled at this this, too… I searched a little about this and here is my understanding (not 100% sure, though) :
A class is like a set of attributes and methods which everything under the class follows. Think about strings and lists which are also “classes”, it’s just python has created rules for us in advance that each class has its specific “built-in” operations. Now we can create our own classes, we need to give them our own rules. Therefore, we have class variables and methods to set rules for the classes we created. After we defined a certain class, when we want something to be considered as that class we created (again, think about str(), int(), etc) , we need to tell python that “this is SomeClass”, so we “instantiate” it and make it an “instance” of that class so that this something later falls under this class and can be applied with various methods we created in the class block.
Again, this is only my own understanding as a fellow learner and I don’t think it is the exact definition.

Also I have a question, what’s so special about the “dunds” there? The “def” process here seems to be the same as what we learnt in the previous lessons where we just define the methods with any name we want.

Maybe @mtf Roy can check my understanding and answer my question? Thanks in advance.

2 Likes

I think you’ve got a pretty good idea. Anything I add will only muddle your understanding.

Some silliness just to see the mechanics…

>>> class Cap_str:
	def __init__(self, value):
		self.value = value.upper()

		
>>> a = Cap_str('watermelon')
>>> a.value
'WATERMELON'
>>> type(a)
<class '__main__.Cap_str'>
>>> isinstance(a, Cap_str)
True
>>> 
1 Like

Thank you for asking what I’ve been also struggling with. But still I don’t get what is the purpose of using init specifically, instead of just using other things. @mtf It would be very appreciated if you can help me out with this. Thanks in advance.

Plus I’ve found a nice youtube programming lecture giving clear explanation of the class and object. Hope you find this helpful as well.


:+1:

The purpose of the __init__() method is to resolve the input parameters into instance attributes (the unique data given to the object).

class Circle:
    def __init__(self, radius):
        self.radius = radius

circle_a = Circle(1)       # instance a
circle_b = Circle(2)       # instance b

print (circle_a.radius)    # 1
print (circle_b.radius)    # 2

Above, circle_a and circle_b are unique instances of the Circle class, and we can see they each have their own radius attribute. self in the above refers to the instance object in context when we poll the attributes or invoke the methods.

from math import pi as PI

class Circle:
    def __init__(self, radius):
        self.radius =  radius
    def circumference(self):
        return 2 * PI * self.radius


circle_a = Circle(1)
circle_b = Circle(2)

print (circle_a.circumference())    # 6.283185307179586
print (circle_b.circumference())    # 12.566370614359172

Note that the last two lines invoke the method with ().

class Circle:
    def __init__(self, radius):
        self.radius =  radius
    def circumference(self):
        return 2 * PI * self.radius
    def area(self):
        return PI * radius ** 2

circle_a = Circle(1)
circle_b = Circle(2)

print (circle_a.area())    # 3.141592653589793
print (circle_b.area())    # 12.566370614359172
2 Likes

Thank you! Your examples are really helpful! :slight_smile:

1 Like

Thanks for this video! We will both learn this eventually!

1 Like