Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8866275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:45:02+00:00 2026-06-14T16:45:02+00:00

i am learning classes in python and i found some functions called magic methods

  • 0

i am learning classes in python and i found some functions called magic methods or special methods that we use them inside the class definitions , the question is the following :

if we add double underlines before and after any original built in function , does that allow us to use all them inside a class to do the same task , for example .

>>> int (3.6)
3

>>> str(3.7)
'3.7'

and we can use str in class as following :

class Character:
    def __init__(self, name, initial_health):
       self.name = name
       self.health = initial_health
       self.inventory = []

    def __str__(self):
       s  = "Name: " + self.name
       s += " Health: " + str(self.health)
       s += " Inventory: " + str(self.inventory)
       return s

   def grab(self, item):
       self.inventory.append(item)

   def get_health(self):
       return self.health

   def example():
       me = Character("Bob", 20)
       print str(me)
       me.grab("pencil")
       me.grab("paper")
       print str(me)
       print "Health:", me.get_health()

example()

the result :

Name: Bob Health: 20 Inventory: []
Name: Bob Health: 20 Inventory: ['pencil', 'paper']
Health: 20

is all built in functions can be magic (special) functions ?

thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:45:03+00:00Added an answer on June 14, 2026 at 4:45 pm

    No. Some built-in functions have a special method for them, some don’t. Also some special methods don’t correspond to a built-in function, and some built-in functions make use of a special method with a different name (e.g., __instancecheck__). You can see which special methods there are in the documentation. This is also a good reference site.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning EF.I have some classes of model and class that inherited from
I'm apparently having some trouble learning how to use classes correctly. I expect the
I'm starting learning classes in PHP. I coded that: class User { function getFbId($authtoken)
So I am learning how to use classes and python and I am creating
I'm just learning classes so I'm trying something basic. I have a class called
I'm learning about packages. I have two classes that are in different packages and
So I was learning about classes and I stumbled upon something I found was
I've been learning about classes in JavaScript, the use of prototype and finally how
I'm still learning how to use classes and I was wondering when should I
I started learning C++, classes, objects, structures and more, but I'm having some problems

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.