Object-oriented python -


i have simple example of class in python:

class song:     def __init__(self, x):         print x  bang=song(['our whole universe in hot dense state,then fourteen billion years ago expansion started, wait...']) 

this works. in book word "object" used when creating new class:

class song(object):     def __init__(self,x):         print x  bang=song(['our whole universe in hot dense state,then fourteen billion years ago expansion started, wait...']) 

this works too. plus if object substituted with, example, x:

class song(x):     def __init__(self,x):         print x  smile=song(['our whole universe in hot dense state,then fourteen billion years ago expansion started, wait...']) 

it doesn't work (nameerror: name x not defined). what's special object, far know isn't reserved word, isn't it? , why code works, while x - doesn't?

this not work because x being treated constructor class. means, basically, in order code work, x defined class.

when use object create class, using template class empty make new type of class. similar thing happens using int or dict. new class inherits properties of type.

since class x not defined, new class cannot use x constructor. therefore, error returned.


Comments