Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMuhammad khan
Python Development Techdegree Student 1,836 PointsAttributes and Class
I am not clear on how a class is instantiated.
He gave an example of age_string = str(25). Here class is str and age_string is an instant of the class str?
Also, in teacher's note, we are calling a class to create and instance. I don't understand why we didn't pass any argument(or do we call it a parameter?) in the class like we did in the above example with str(25). we passed 25 to a str class.
new_instance = New_Class() ----> New_Class(?)
1 Answer
Mark Tripney
8,666 PointsHey, Muhammad.
Yes, you're right β remember, everything in Python is an object: strings, integers, functions, etc. So, age_string = str(25)
is instantiating (creating) an instance from the str
class, called age_string
. On instantiation, the str
class is passed an argument of 25
.
On your other point, it's simply not necessary to pass arguments when instantiating an object. We can write a class like so...
class NewClass:
pass
... and instantiate an object like so...
my_object = NewClass()
This is a pretty uninteresting object and class, but valid! (For clarity, pass
, in the above example, is a null operation βΒ it's a useful placeholder as, when it's executed, nothing happens.)