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 6575277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:22:01+00:00 2026-05-25T15:22:01+00:00

In Java, objects are instantiated with a complete copy of the functions from the

  • 0

In Java, objects are instantiated with a complete copy of the functions from the class linked to them. This is one of the reasons that the Spring Framework has been so successful. Spring helps you cut down on the memory that the Java VM uses if you create many temporary data objects, and other service objects that are served up by Spring as singletons that effectively carry all the functions.

I was just wondering if this true in Python? It seems like it isn’t. But that means that if you mess with dict for an object, you are changing all that function for all copies of that class, right?

For example:

class MyObj:
  a = 23

  def __init__(self, b):
     self.b = b

  def __add__(self, c):
     return self.b + c

If I create an array of MyObj, is there one instantiation of __add__ for each, or just one for all of them?

  • 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-05-25T15:22:01+00:00Added an answer on May 25, 2026 at 3:22 pm

    There is just one instance of the function, which is stored inside the class. The function gets called with a reference to a class instance as the first argument, which is traditionally called self.

    So, here are three equivalent ways to call the .__add__() method function:

    >>> x = MyObj(2)
    >>> MyObj.__add__(x, 3)  # we pass in a reference to x explicitly
    5
    >>> x.__add__(3)  # method call implicitly passes reference to x
    5
    >>> x + 3  # overloaded operator implicitly passes reference to x
    5
    

    Also, in Python, the “type signature” of a function is not part of the function’s name. The only .__add__() method you get is the one you declared. In Python you would simply write your code to make the one function do all the different jobs you might want it to do. For example, if you wanted .__add__() to convert a string into an integer to make x + "3" return 5, you would do it like so:

    class MyObj(object): # see note 0
        a = 23
        def __init__(self, b):
            self.b = b
        def __add__(self, other):
            return self.b + int(other)  # see note 1
        def a_add(self, other):
            return MyObj.a + other
    

    Note 0: It is best to declare your class as inheriting from object. In Python 3.x, your class will inherit from object whether you declare it this way or not, but in Python 2.x you will get an “old-style class” unless you declare it with (object) after the class name.

    Note 1: we don’t bother to check the type of the other argument; we just try to coerce it to an int value. This is “Duck Typing” in action. Now not only will a string be coerced to an integer, but anything that can successfully be coerced to an int will work.

    Sometimes, to make one function do multiple jobs, you may need extra arguments. You can make them optional so that you don’t need to specify them every time you call the function. You can read more about this in a Python tutorial; here’s one place: http://diveintopython.net/power_of_introspection/optional_arguments.html

    Finally, in Python, you need to explicitly use MyObj.a to refer to that class variable a. If you just use a in a member function, you will get the usual name resolution rules, which will look in the global namespace; the object namespace isn’t special.

    You could also get to a class variable like so:

    def a_add(self, other):
        cls = type(self)  # cls will be set to the class
        return cls.a + other
    

    But this will only work if it is a new-style class that inherits from object!

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

Sidebar

Related Questions

I want to have immutable Java objects like this (strongly simplified): class Immutable {
I'm writing a Java application that will instantiate objects of a class to represent
From The Java Tutorials : In Java, a class can inherit from only one
When an object is instantiated in Java, is it bound to the thread that
I am working on generating Java objects from an XSD file using JAXB 2.1.
We're building some Java objects that are exposed via BlazeDS to our flex client
Suppose I have a Java class that has 100K worth of method code containing
Java/OO newbie question: main instantiates Track class. Now I want that object-- track1 --to
Why does it happen that a command line argument passed to a Java class
I have a Java class called CreateThread which creates one thread, everytime an object

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.