Python Forum
Special Methods - what are they exactly?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Special Methods - what are they exactly?
#1
Hello Everyone,
I am trying to fully grasp what special methods are. In general, methods are functions which are defined inside a class, at least I believe. Python has many reserved words and built-in functions like print(), add(), type(), etc.
Special method are predefined methods (not custom methods the user can create). They are used inside a user defined class to improve it...But what do they do exactly?
I run into the concept of overloading (making an object do multiple different things)

For example, if we create a class adn include the method __init__ to initialize the class, this methods acts automatically to create class attributes (the characteristics of any instance of the class) even without call this special method __init__.

Other special methods are __main__ , __str__ , __new__ but I guess these behave like other non special methods and need to be called after creating an instance from the class..

Do special methods essentially allow us to extend the capabilities of built-in functions to objects that the built-functions themselves are not designed for? For example, dir(5) tells us which methods can be used on integers but not strings....But it may be possible to use one of the listed methods for strings too within a class if we include the special method...

Thanks!
Reply
#2
This explains them pretty well: https://www.python-course.eu/python3_magic_methods.php
nilamo, ibreeden, bytecrunch like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  all of attributes and methods related to a special type akbarza 4 425 Jan-19-2024, 01:11 PM
Last Post: perfringo
  Special Methods in Class Nikhil 3 2,224 Mar-04-2021, 06:25 PM
Last Post: Nikhil
  How to Create Very Very Special Class with too many magic methods ? harun2525 5 4,305 Apr-13-2017, 10:18 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020