DEV Community

James Hubert
James Hubert

Posted on

Intro to Python - Creating an Instance of a Class

Hi there. I'm a New York City based web developer documenting my journey with React, React Native, and Python for all to see. Please follow my dev.to profile or my twitter for updates and feel free to reach out if you have questions. Thanks for your support!

Today's boot.dev lesson in Object Oriented programming with Python centered around how to use classes to create instances, which are objects.

Like Javascript, in Python everything is an object. But classes have some syntactical sugar that makes creating an class feel different, which is the case with the other data types as well.

To create a class in Python simply use the class keyword and give it a name with an upper-case letter, like so:

class Person:
Enter fullscreen mode Exit fullscreen mode

Typically, classes will have a constructor function where values unique to each instance of the class are set when the instance is created. The constructor function in Python uses the __init__ keyword and takes the self argument, which will exist on the class scope:

class Person:
    def __init__(self):
        self.health = 100
Enter fullscreen mode Exit fullscreen mode

The above constructor function sets the health for each instance of the Person class at 100 when it is created. If we want to give each instance some unique values then we can pass in those values as arguments:

class Person:
    def __init__(self, name):
        self.health = 100
        self.name = name
Enter fullscreen mode Exit fullscreen mode

Then when we instantiate the class we can give each instance its own unique name like so:

james = Person("James")
Enter fullscreen mode Exit fullscreen mode

Notice that we don't have to pass in the self variable, and the first variable passed to the class when creating an instance is the second argument of the constructor function. This took some getting used to for me. But when you consider that the self argument is specially scoped to the class, it begins to make some sense why Python would be built this way.

Now let's create a method for the class, which is a special function defined within an object, or the class in this case.

class Person:
    def __init__(self, name):
        self.health = 100
        self.name = name

    def say_my_name(self):
        print(f"My name is {self.name}")
Enter fullscreen mode Exit fullscreen mode

The above say_my_name method accepts the self argument which is defined only internally within the class. To call it we can call james.say_my_name() which will print My name is James for the james instance of the Person class.

To create a method on a class which accepts a unique argument to be defined on each method call, we can simply pass in another argument when we define the method, like so:

class Person:
    def __init__(self, name):
        self.health = 100
        self.name = name

    def say_my_name(self):
        print(f"My name is {self.name}")

    def say_weather(self, weather):
        print(f"Hi my name is {self.name}. I love this {weather} weather we are having, don't you?")
Enter fullscreen mode Exit fullscreen mode

Then after the class is instantiated, you can call the say_weather method and pass in any string you want to it.

james.say_weather("sunny")
Enter fullscreen mode Exit fullscreen mode

This will print:

Hi my name is James. I love this sunny weather we are having, don't you?
Enter fullscreen mode Exit fullscreen mode

These are the basics of creating a class in Python and instantiating it.

If you like projects like this and want to stay up to date with more, check out my Twitter @stonestwebdev, I follow back! See you tomorrow for another project.

Top comments (4)

Collapse
 
mohammadparsajavidi profile image
mohammadparsa-javidi

very useful👌❤

Collapse
 
jameshubert_com profile image
James Hubert
Collapse
 
mitexleo profile image
Mitex Leo

Thank you for posting. Helpful for refreshing my knowledge.

Collapse
 
jameshubert_com profile image
James Hubert

Thanks @neonota !!