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
5 Likes

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.

10 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
>>> 
5 Likes

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.

1 Like

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:
15 Likes

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 * self.radius ** 2

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

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

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

1 Like

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

1 Like

how we could use radius without self in second exmp in area method , based on what I understand
I tested it and got and error :

Traceback (most recent call last):
  File "script.py", line 13, in <module>
    print (circle_a.area())    
  File "script.py", line 8, in area
    return 3.14 * radius ** 2
NameError: name 'radius' is not defined

That is my error. I’ve corrected the example to include self. Sorry about that.

very confusing. you always use foo or bar. what is that?

Generic names. Nothing more. They still bear the same meaning and represent the same concept. Here, they only represent the concept, not some other abstract.

thanks for recommending this video, I am going through it now including his init method as I am struggling a little bit with the this module! :sweat_smile:

Hi @mtf here your said self.radius =radius, i know the second radius after the assignment operator is the one that we passed as an argument while calling the method(or instantiating a class),
but what about the first one that is self.radius. i though if the class has a variable(attribute) named radius we can link it to that particular instance with the self
but here i dont see any attribute of class Circle with name radius and the code is running fine without any error. can you please explain why.

and i had an other doubt that can we instantiate an object of class in the same class itself? if so how can we do it.

explanation with examples will be more helpful. Thank You.

self.radius

The attribute name is radius, as in,

a.radius
b.radius

self is just the special property that denotes the owner instance, as in a and b above.

1 Like