0

I see that this question has been asked before, but believe me when I say that it is very frustrating trying to understand this concept and not being able to move on because of it. I really appreciate anyone helping out, so please let me know if I am not being clear with certain things.

I have been trying my best to move on and start coding in Python, as I am confident I understand the structure of the language when it comes to using flow control and trying to manipulate data types, however, I still do not seem to get a the grasp of OOP.

I have been doing quite a lot of research trying to understand what the difference between an Object and an Instance is and how those are invoked in a program those two terms is. As far as I know, they are the exact same thing, however, I get really confused when people start using really advanced terms to describe the concept. I understand that instantiating an Object is an Instance of a class, but what I do not understand is the precedence of one another.

For example:

class MyClass:
    #Attributes?
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def happy_birthday(self): 
        return 'You are {}, happy birthday {}.format(self.age, self.name)'
#someone said that the object is the instance, however, I do not understand that part.

#Instantiating an object?
boy1 = MyClass()

#Is this also an object or an instance if I call the function?
boy1.happy_birthday()

I have also been searching for examples that mark which parts are which in the code.

codeHunter
  • 47
  • 6
  • 2
    When you typed in this question duplicates must have been shown up as suggestion, isn't it? – wp78de Nov 10 '17 at 04:32
  • 1
    Well, there's the "everything is an object in Python" phrase so `boy1` is an object that happens to be an instance of `MyClass`. – roganjosh Nov 10 '17 at 04:34
  • Your last line is perhaps the most confused part. Say your class was `Table` and we create `table1= Table('mine')` where `'mine'` is a string that can be passed to a new `Table` object to indicate possession. So, now we have a unique table - the one in my room. `table2 =Table('yours')`. If I spill coffee on my table (`table1.spill_coffee()`), would it spill on yours too? They're both tables, and both objects, but only _my_ table (the instance of the table in my house - `table1`) is affected. And my table is still the same physical object after the spill, it wasn't re-made by the spill. – roganjosh Nov 10 '17 at 04:49
  • 1
    I think both the duplicates are confusing things with JS-like explanations. Javascript doesn't have classes proper, just "object prototypes". In Python every object is an instance of a class. [This](https://stackoverflow.com/a/2885485/3650362) is the best of the linked answers IMO. – trent Nov 10 '17 at 04:54
  • While I appreciate the help of those who helped me get the concept, I also would like to express how rude some community members are. I wonder why there is an abusive and rude Flag option. Again, thank you to all who helped me out. – codeHunter Nov 10 '17 at 14:30
  • PS if the ones saying that there were options learn how to read plain text, they would see that I actually did research on the matter and tried looking in books, online and asking myself. But again, thanks for the rude comments. I hope you are still not upset because your pet rock died. – codeHunter Nov 10 '17 at 18:52

1 Answers1

-4

The term object is used when talking about instances of a class in general. For example someone might tell you to "Make sure to handle exceptions for Car class objects in your code." when they want you to check whether a variable is something made with a Car class constructor.

The term instance is used when talking about a specific example of an object. For example, you can refer to a Car object with the variable name "toyota" as a Car class instance, which might have a unique set of field values.

But I also feel in general people are not that picky with differentiating between the term object and instance so I feel there's no need to stress too much about this.

Tatsuya Yokota
  • 444
  • 3
  • 15
  • 1
    Peer pressure recommends you to delete your answer. This will also remove the negative repo for you. – wp78de Nov 10 '17 at 06:04