-1

I have a simple example of class in python:

class song:
    def __init__(self, x):
        print x

bang=song(['Our whole universe was in a hot dense state,Then nearly fourteen billion years ago expansion started, wait...'])

This works. But in another book the word "object" is used when creating a new class:

class song(object):
    def __init__(self,x):
        print x

bang=song(['Our whole universe was in a hot dense state,Then nearly fourteen billion years ago expansion started, wait...'])

This works too. Plus if object is substituted with, for example, x:

class song(x):
    def __init__(self,x):
        print x

smile=song(['Our whole universe was in a hot dense state,Then nearly fourteen billion years ago expansion started, wait...'])

It doesn't work (NameError: name x is not defined). What's so special about object, as far as I know it isn't even a reserved word, isn't it? And why the code with it works, while with x - doesn't?

dhke
  • 15,008
  • 2
  • 39
  • 56
parsecer
  • 4,758
  • 13
  • 71
  • 140
  • 4
    You might want to have a look at [Inheritance in Python](https://docs.python.org/2/tutorial/classes.html#inheritance) and then at [old vs new style classes](https://wiki.python.org/moin/NewClassVsClassicClass) – dhke Jul 09 '15 at 22:12
  • `NameError` usually refers to an attempted reference to something that does not exist. Just for future reference. – bcdan Jul 09 '15 at 22:17

3 Answers3

1

This does not work because x is being treated as a constructor class. This means, basically, that in order for your code to work, x would already be defined as a class.

When you use object to create a class, you are using a template class that is empty to make a new type of class. A similar thing happens with using int or dict. The new class inherits the properties of the type.

Since the class x is not defined, the new class cannot use x as the constructor. Therefore, that error is returned.

bcdan
  • 1,438
  • 1
  • 12
  • 25
0

Because object is the base object that you are inheriting from. x does not exist as an object and therefore cannot be inherited from

You could do:

class x(object):
    def __init__(self, item)
        self.item = item


class song(x):
    def print(self):
         print(self.item)


bang=song(['a bunch of text']) #why is this a list?

bang.print()

And there you have it inheritance - so many x's make this confusing

Sina Khelil
  • 2,001
  • 1
  • 18
  • 27
0

First of all you should get acquainted with inheritance

As shown in other answers it is possible to state class song(x):, if x is a class on its own. By doing this the songclass will inherit the methods from the base class x.

Now, the reason for a class declaration to inherit from object dates back to python2.2. These type of class declarations are called : New style classes.

They have a different object model to classic objects and have a set of properties and features that are not present in classic objects. Some examples of this are the @property keyword, the super() method. More details about their difference can be found here, but it's also widely discussed on Stack Overflow: here.

It is advised to use these new style classes and so to let your base classes inherit from the object class.

Community
  • 1
  • 1
DJanssens
  • 17,849
  • 7
  • 27
  • 42