0

Below is some code from Learn Python The Hard Way Exercise 40: Modules, Classes, And Objects I can't seem to figure out why is there a parameter for class MyStuff(object). What's the reason to put object in the parentheses?

class MyStuff(object):

    def __init__(self):
        self.tangerine = "And now a thousand years between"

    def apple(self):
        print "I AM CLASSY APPLES!"

I have looked through some of the articles here on StackOverflow but failed to understand what's new object and old class object. Could someone please explain the meaning of object in the class definition? What happens if I leave that the parentheses empty when I declare my class?

Also there is a (self) argument for the 2 functions in the code.
In the book it says "self" is an empty object python created while instantiating an object with class.
But why stating the argument before the actual use of it??

I have been learning programming for 2 weeks only so I apologize if the question is too superficial, thank you for your time~

Aaron D
  • 7,540
  • 3
  • 44
  • 48
MorboRe'
  • 151
  • 10
  • possible duplicate of [Python class inherits object](http://stackoverflow.com/questions/4015417/python-class-inherits-object) – Aaron D May 15 '15 at 06:12
  • yah, i've read that but I got no clue of what that article is talking about – MorboRe' May 15 '15 at 06:15
  • 1
    You should also take a look at this: https://wiki.python.org/moin/NewClassVsClassicClass – konart May 15 '15 at 07:58

1 Answers1

2

As per your comment, I see you are using Python 2. In Python 2.2 they introduced "New-style" classes, but kept "Classic" classes for backwards-compatibility reasons. In Python 3.0 or newer, classic classes are gone - every class is a new-style class (regardless of syntax).

For python 2.2-2.7, syntactically a new-style class is declared by subclassing from object explicitly (unless it has a different parent class):

class MyStuffNew(object):
    a=1

while omitting the object reference creates a classic class:

class MyStuffClassic():
    a=1

Functionally, they work almost the same, but there are a few differences between them in the builtin language definitions. For example, New-style classes introduced a builtin class method __mro()__ (method resolution order) which is used by the interpreter to work out which method (in the class inheritance heirarchy) you meant to call. This inbuilt method is missing in old-style classes (and the method resolution order is different, which can lead to some unexpected behaviour). For more information about New-style vs Classic classes, please read this Python Wiki page.

As mentioned above, in Python3 or above, the syntax used doesn't matter; all classes are new-style. However, many coders will use the class MyClass(object): syntax to maintain code compatibility with Python2.7. - Please see this answer.

In any version of the Python language, class MyChildClass(ParentClass): defines an inheritance relationship - MyChildClass is a child of ParentClass and will inherit all of its methods and fields.

The self is the way Python knows this is a member method and not a static function of your class. You can only access member fields and methods from within your class by using self.field and self.myMethod(var1,var2), etc.

When you actually call these functions from other places in your code, you don't pass a value to self - it is the variable that you are using. For example:

stuff = MyStuff()
stuff.apple()
# outputs "I AM CLASSY APPLES!"
print stuff.tangerine 
# outputs "And now a thousand years between"
stuff.tangerine = "Something else."
print stuff.tangerine
# outputs "Something else."
stuff2 = MyStuff()
print stuff2.tangerine
# outputs "And now a thousand years between"

If you don't include self in the method definition, calling mystuff.apple() will result in a runtime error because implicitly it's the same thing as calling MyStuff.apple(mystuff) - passing the instance of your MyStuff class into the apple function.

Community
  • 1
  • 1
Aaron D
  • 7,540
  • 3
  • 44
  • 48
  • I think the book Learn Python The Hard Way really isn't worth my money since it raises question but never give the answer to the question. I have no way of finding if the answer I found online is correct or not. Thanks for the time you spent to explain my question. – MorboRe' May 16 '15 at 04:14
  • You're welcome. If you feel I answered your question, please click the tick on the top left of my answer to mark it as 'accepted'. If not, please let me know what is unclear and I'll edit it to answer if I can. – Aaron D May 16 '15 at 05:48