1

So I've been working to replace PHP with Python in my career for a bit. So I'm using WebPy with WSGI in apache, and all is working well, but I'm still learning the language, and can't find a simple explanation for this. So, while messing around, trying to get other classes methods to work in other classes, I came across some instructions that showed the first class being instantiated with (object) appended to the class name. It turns out that this works and lets me pass data through to that other class. Can someone tell me why this code will work?

And by work, I mean, it appears to me that if the first class doesn't specify (object) during its definition, then data can't be passed into that class? Is this right?

class MyClass(object):
    def function1(self, num1, num2):
        return num1 + num2

class index:
    def GET(self):
        form = web.input()
        num1 = form.number1
        num2 = form.number2

        MyClass.function1(num1, num2)

I'm really trying to understand this. I mean it's great that I have it working (this is a code sample, not my actual project), but it will help if I understand WHY it's working. Thanks, I'm sure this is probably an easy question.

IAteYourKitten
  • 968
  • 1
  • 6
  • 13
  • If you're confused by `MyClass(object)` don't be. Those are called new style classes and basically only live in Python 2. Py3 classes are all new-style classes by default. See [here](http://stackoverflow.com/questions/54867/what-is-the-difference-between-old-style-and-new-style-classes-in-python) i.e. for more details. If you're wondering why you don't need `__init__` in `MyClass` and don't need to instantiate/initialize a class object, it's because the method is approximately about as close to an static method from C-family as it gets. – ljetibo May 11 '15 at 12:20
  • check this link:http://stackoverflow.com/questions/10044321/class-classnameobject-what-sort-of-word-is-object-in-python – Jay Venkat May 11 '15 at 12:21

3 Answers3

3

There are two types of classes in Python 2: the old style and the new style. The new style classes are created by subclassing the object. The difference is not that huge, in fact you will hardly notice it, if you don't use multiple inheritance and type comparisons with type, i.e.

class old_style:
    def __init__(self):
       pass

class new_style(object):
    def __init__(self):
        pass

old_style_instance1 = old_style()
old_style_instance2 = old_style()
new_style_instance1 = new_style()
new_style_instance2 = new_style()

type(old_style_instance1) == type(old_style_instance2)

Returns False

type(new_style_instance1) == type(new_style_instance2)

Returns True

Regarding your code. You tend to use instance methods like if they are class methods, i.e. the methods MyClass.function1 and index.GEThave an argument called self, hence they can only be called from class instances, not the classes themselves. self is a special namespace created by a special method (__new__) upon instance initialization, e.g. myclass_instance = MyClass(). In case you want to use class methods or static methods that can be called from classes, you should declare them in a different way.

class MyClass(object):
    @staticmethod
    def function1(num1, num2):
        return num1 + num2

class index:
    @staticmethod
    def GET():
        num1, num2 = get_numbers_somehow()
        MyClass.function1(num1, num2)

In this case the code will work and this has nothing to do with MyClass inheriting from the object class. You should consider reading about Python classes and instances to understand the difference. BTW, the @ syntax is used for a thing called decoration. Read about Python decorators if you are going to use it a lot.

Eli Korvigo
  • 10,265
  • 6
  • 47
  • 73
1

Don't be confused with this. It's new style object that introduced several new feature in python 2.2. It the default behaviour in Python 3. but you need to have the object in Python 2

Please refer to : Python class inherits object

RPresle
  • 2,436
  • 3
  • 24
  • 28
  • I actually read that answer before posting this one. So, why can I pass data through this class when I put 'object' in there? Is there a simple thing that (object) does to make it work properly? – IAteYourKitten May 11 '15 at 12:25
  • Oh wait, I found a bit to help my understand... So new style is a way for them porting new syntactic or other elements from newer py development to python 2? – IAteYourKitten May 11 '15 at 12:28
  • 1
    That's what I think. It is a way to backport feature from Py 3 to Py 2. See [https://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes](https://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes] – RPresle May 11 '15 at 12:32
1

I think this bit of introspection will help you understand:

given any object x you can see what functions and data members are available by typing in dir(x). Try this stuff out:

>>> object
<type 'object'>      
>>> dir(object)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> 
>>> 
>>> class A:pass
...              
>>> A
<class __main__.A at 0x7f2ef1bdf2c0>
>>> 
>>> dir(A)
['__doc__', '__module__']    #this is the only stuff attached to the class 'A'
>>> 
>>> class B(object):pass   #the brackets imply inheritance
... 
>>> B
<class '__main__.B'>
>>> 
>>> dir(B)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

B has all the things A had (because there is some default stuff all classes get), and also has all the stuff object has (because it inherits from object)

ALSO. in your code, the line MyClass.function1(num1, num2) wont work. Try it and see

Sheena
  • 15,590
  • 14
  • 75
  • 113