Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I would assume that instances of the same class would actually share their methods, and just have save different attributes in their namespace. How often do you arbitrary add methods to a single instance?

Yet for example in Python that's not the case. Instances' methods are all different objects. Even if you decorate them as @classmethods, (or use __slots__) they seem to be different objects in memory.

Can someone give me a definition of what I'm describing here, or even given same details on why this model was chosen? What happens in other languages?

share|improve this question

closed as unclear what you're asking by gnat, MichaelT, Dynamic, World Engineer Apr 8 '14 at 0:09

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers 3

The relevant terms here are dynamic and static typing.

With static typing, all the types are fixed and do not change during the execution time of your program. This is what traditional languages like C/Java/etc are doing. The advantage for compiled languages with static typing is that they know, that each object of your class will have the same methods, and as such they are free to optimize with that in mind. In particular, each method implementation is only stored once in memory and merely the object attributes are stored per individual object. For an object, you just need to store a single pointer to its class' method table.

On the other hand, languages like Python offer dynamic typing, where a type can change during runtime. For example, in Python it is possible to add a method to a class. Once you start modifying the type though, you are no longer guaranteed, that a once compiled version of your method(s) is still valid, so it's more troublesome to store these methods. If your language even allows you to add methods to an object's class per individual object, than no two objects can share their type.

There are still a lot of optimizations possible with dynamic typing, but as a rule of thumb, statically typed languages have a much easier time for optimizing, because they have a lot of guarantees regarding their type system. On the other hand, the programmer is forced to adhere to the type system just as well, in contrast to the freedom of a dynamically typed language. Both sides have advantages and disadvantages, but these are outside of the scope of your question.

share|improve this answer
    
"in Python it is possible to add a method to a class", I think you meant to "an instance of a class". –  Mahdi Apr 2 '14 at 6:19
1  
"On the other hand, the programmer is forced to adhere to the type system just as well, in contrast to the freedom of a dynamically typed language." I know this is sort of tangential, but contrary to popular belief dynamic typing is a restriction, not a freedom. "Dynamically" typed languages are statically typed, but have only one type; it's equivalent to declaring every variable as having type Object in Java or C#. You're deprived of the ability to assert a variable has a different type. Additionally, objects in e.g. Python are maps in disguise. –  Doval Apr 2 '14 at 11:31
    
@Mahdi I'm not sure if that's what he meant, but the statement is correct. You can add methods to classes quite easily in Python. –  Doval Apr 2 '14 at 11:31
    
While this answer does answer the question, I can't upvote because it seems to be biased for dynamic typing and against static typing. –  Euphoric Apr 2 '14 at 11:35
1  
@Mahdi Adding methods to a class in statically-typed languages is generally not possible. Even when you have mechanisms like mixins or traits or extension methods, you're only limited to adding things. In Python you can also remove or change methods of a class, and you can do so at runtime. They're both pretty good examples, I think. –  Doval Apr 2 '14 at 16:49

OOP concepts do not change if you compare them in different programming languages though the way it gets implemented might vary. I have been working on C++ for 2 years and before that I learnt and worked in Java as well.

Here is how you can look at it.

Classes are the blueprints based on which objects are generated. In other words class can be seen as generic logical entities and objects are physical entity. For example: shape is a generic logical entity and circle is a physical entity.

Every class contains data and methods(function) to manipulate that data. for ex: shape has length, size etc as data and volume(), Area() etc as methods.

Now as far as I understood your question you are asking about how or when methods get differ for different object/instances. (correct me).

Now to answer that we need to understand Polymorphism which is an OOPs concept. Polymorphism has two variants static and dynamic. Static polymorphism is implemented through function/method overloading and Dynamic polymorphism is implemented through function/method overriding.

Static binding: Multiple methods, same name, same scope, different parameters. Gets resolved at compile time.

Dynamic binding: Multiple methods, same name, different scope, same parameters. Gets resolved at runtime.

Now if two instances(say O1 O2) work in such a way that they eventually call different methods either through static binding or dynamic binding. That means instance O1 will call different methods when you compare it with O2.

I hope that makes some sense, post doubts if any.

share|improve this answer

Even in languages that allow dynamic modification of a single instance, like Python or Ruby, the code of methods still only exists in memory once. The main difference between those languages and those where a single instance cannot be changed (like Java or C++) is that in the static languages, each instance conceptually holds a single reference to a list of supported functions that is shared between all instances (in concrete implementations this is often called the vtable), whereas in dynamic languages each instance needs its own table. (As an optimization, unmodified instances could share the table and only create their own versions when they get modified.)

In either case, however, those tables will contain references to the code of the actual methods, and that code will be shared. Unless your language is so dynamic that it can modify the actual code at runtime.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.