-2

Here is my code. What is the simplest way to call the example function?

class Sample():
    def example(x, y):
        z = x + y
        print(z)
        return z

I would really appreciate a nice description of it.

Djangodev
  • 450
  • 3
  • 17
  • `example` is not a function, it is a `method`; you need to instantiate the class and call it with that *object*. (You should also add a variable called `self` to the example's signature, because every method will receive it as as the first argument to know the exact object it is operating on.) – Nishant Jan 24 '22 at 07:16
  • 1
    @Nishant A method **is** a function. – Kelly Bundy Jan 24 '22 at 07:23
  • @Nishant I am still learning these things, it is very confusing for me. Let me know if I am wrong anywhere. That's why I asked for a nice description. – Djangodev Jan 24 '22 at 07:23
  • 1
    @NoorulainIbrahim, read the difference between a method and function: https://stackoverflow.com/questions/155609/whats-the-difference-between-a-method-and-a-function. That should clear your doubts :). You are having a basic OOPS doubt, so don't worry ... just keep at it. – Nishant Jan 24 '22 at 07:44
  • @KellyBundy A method is a special type of function, so I felt it is a better to know the difference - especially when the doubt is about how to call it. – Nishant Jan 24 '22 at 07:45
  • Thanks, @Nishant but I have many more questions to ask about it. There are more pieces of code that require a nice decription. I want to ask that too – Djangodev Jan 24 '22 at 07:57
  • @Nishant I already checked the link you shared but It didn't clear me up. Here I can comment to ask my confusion. – Djangodev Jan 24 '22 at 07:58

1 Answers1

1

You can call it directly:

Sample.example(1, 2)
Scene
  • 489
  • 4
  • 16
  • 1
    What makes you think it's static? – Kelly Bundy Jan 24 '22 at 07:31
  • I have no idea what static is, I need to learn this thing and someone closed the question so I am really demotivated to ask questions here @KellyBundy – Djangodev Jan 24 '22 at 07:35
  • 1
    @KellyBundy I supposed it'd be better described as a simple function that lives inside the Sample class namespace in Python 3. Thanks for the heads-up – Scene Jan 24 '22 at 07:41