0

I don't know if there is any difference between these two Meta declarations:

class Car(models.Model):
    #...

    class Meta(object):  # <------- (1)
       #...

    class Meta:          # <------- (2)
       #...

Which one is correct and preferred? Does it depend on the usage?

I use python 2 and 3, and Django 1.7+.

masoud
  • 55,379
  • 16
  • 141
  • 208
  • I've never seen the first example `Meta(object)` used. Where did you get it from? – yuvi Feb 14 '15 at 13:18
  • I have a source code which uses python 2.7 and Django 1.7. It uses the first one to declare `verbose_name` and `verbose_name_plural`. It works. – masoud Feb 14 '15 at 13:19

1 Answers1

2

Classes which don't inherit from anything are called old-style classes

class old_style:
    pass

Other classes which inherit from object are called new-style classes

class new_style(object):
    pass

classes who inherit from new-style classes are also new-style because at the end they inherit from object:

class new_style(object):
    pass

class new_style2(new_style):
    pass

In python 3, all classes are new classes(inherit from object), therefore in python 3:

class e(object):
    pass

class d:
    pass

e == d

You can read more about their differences/usages in this question

Community
  • 1
  • 1
no_name
  • 742
  • 3
  • 10
  • So, you say both of them are the same in action? This [official reference](https://docs.djangoproject.com/en/1.7/ref/models/options/) is using the old-style. – masoud Feb 14 '15 at 13:25
  • 1
    New-style classes contain some bulit-in methods/attributes but they are slower to create, old-style classes only contains __doc__ and __module__ but they are much more faster to create ---- You can read more in the question I've linked ---- If you're using python 3 then just ignore all of this, since old-style classes only exist in python 2 – no_name Feb 14 '15 at 13:26