Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

Can anyone tell me, In python, how to call an instance of a class?

closed

4 Answers

0 votes
by (15.4k points)
selected by
 
Best answer
To invoke or use an instance of a class in Python, you can employ parentheses following the class name, just like calling a function. Consider the following example:

class MyClass:

    def __init__(self, name):

        self.name = name

    def greet(self):

        print(f"Hello, {self.name}!")

# Creating an instance of the class

my_object = MyClass("John")

# Invoking a method on the instance

my_object.greet()

In the above illustration, we define a class called MyClass with an initializer (__init__) that accepts a name parameter. By calling the class name as if it were a function with the desired arguments, we create an instance of MyClass referred to as my_object. Subsequently, we can invoke methods on the instance, such as greet(), by using dot notation (my_object.greet()), thereby executing the code within the method for that specific instance.
0 votes
by (26.4k points)

You can call the instance using below code:

o = object() # create our instance

o() # call the instance

But this will give error:

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'object' object is not callable

How might we call the instances as expected, and maybe receive something helpful in return? 

We need to execute Python's unique technique, __call__!

class Knight(object):

    def __call__(self, foo, bar, baz=None):

        print(foo)

        print(bar)

        print(bar)

        print(bar)

        print(baz)

Now, Instantiate the call:

a_knight=knight()

Now, try to call the instance of class

a_knight('ni!', 'ichi', 'pitang-zoom-boing!')

which will prints:

ni!

ichi

ichi

ichi

pitang-zoom-boing!

Are you interested to learn the concepts of Python? Join the python training course fast!

0 votes
by (25.7k points)
To call an instance of a class in Python, you can simply use parentheses after the class name, similar to calling a function. Here's an example:

class MyClass:

    def __init__(self, name):

        self.name = name

    def greet(self):

        print(f"Hello, {self.name}!")

# Creating an instance of the class

my_object = MyClass("John")

# Calling a method on the instance

my_object.greet()

In the example above, we define a class MyClass with a constructor (__init__) that takes a name parameter. We then create an instance of MyClass called my_object by calling the class name as if it were a function with the desired arguments. Finally, we can call methods on the instance, such as greet(), using dot notation (my_object.greet()), which will execute the code within the method for that specific instance.
0 votes
by (19k points)
To use an instance of a class in Python, simply call the class name followed by parentheses, similar to invoking a function. Here's a concise version:

class MyClass:

    def __init__(self, name):

        self.name = name

    def greet(self):

        print(f"Hello, {self.name}!")

my_object = MyClass("John")

my_object.greet()

In the brief example above, we define the MyClass class with an __init__ method and a greet method. We create an instance of MyClass called my_object by calling the class name with the desired arguments. Finally, we can call the greet method on the instance using dot notation, which executes the method's code specifically for that instance.

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
asked Oct 8, 2019 in Python by Sammy (47.6k points)

Browse Categories

...