-7

There are a lot of tutorials and sites online that explain the keyword self in Python, but none that explain what it means to call self(param1, param2). What is this line of code doing or what function is it calling in the class?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Anonymous
  • 93
  • 8
  • 6
    `self` is not a keyword in Python, so if those resources were saying it was a keyword, then they are not good resources – juanpa.arrivillaga Apr 20 '23 at 21:24
  • 1
    Possible duplicates: [What is self() in python?](/q/54555141/4518341), [How can I make a class object callable in Python?](/q/70916141/4518341). See also [What is the purpose of the `self` parameter? Why is it needed?](/q/2709821/4518341) if you need any clearing up about how it's not a keyword. – wjandrea Apr 20 '23 at 21:44

2 Answers2

1

A class can define a special method __call__(), which allows its instances to be used as functions. If the class does this, methods can use self() as a function. So

self(param1, param2)

is equivalent to

Classname.__call__(self, param1, param2)

(where Classname is the class of self).

Barmar
  • 741,623
  • 53
  • 500
  • 612
-3

self is the default variable of a class. Think of it like the default word for a class.

Take this time class for example:

class time:
  def __init__(self, hour, minute, second):
    self.hour = hour
    self.minute = minute
    self.second = second

  def __repr__(self):
    return "%02d:%02d:%02d" % (self.hour, self.minute, self.second)

f = time(10, 30, 57)
print(f.__repr__())

Using self, we can assign our attributes to the time class specifically, that means if we have another attribute named self.hour in another class, it would be completely different unless it was the same value or used the super() function.

Ex:

class time:
  def __init__(self):
    self.hour = 1
    self.minute = 5
    self.second = 32

  def __repr__(self):
    return "%02d:%02d:%02d" % (self.hour, self.minute, self.second)


class MoreTime:
  def __init__(self):
    self.hour = 6
    self.minute = 0
    self.second = 2
  
  def __repr__(self):
    return "%02d:%02d:%02d" % (self.hour, self.minute, self.second)



f = time()

g = MoreTime()


print(f.__repr__())

print(g.__repr__())

Output:

01:05:32

06:00:02

Note: You do not HAVE to use self. It is highly recommended to use it because it is the word most used to symbolize an instance of a class. It is used for better readability and its just a good habit to learn. Do note that self is not a keyword.

class main:
  def __init__(bee):
    bee.string = "Hello World"
  def run(bee):
    return bee.string

d = main()
print(d.run())

As you can see, you can define self as whatever you want in the initializer. I recommend not using this code above and just use self.

Ray953
  • 47
  • 5
  • The question is about *calling* `self`, which this doesn't cover. – wjandrea Apr 20 '23 at 22:04
  • BTW, I would avoid calling a class `main` since that's the conventional name of the main function (AKA [entry point](https://en.wikipedia.org/wiki/Entry_point)). You could do `Main` instead, though that might still be confusing -- depends on the context. – wjandrea Apr 20 '23 at 22:04