Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object parameter in python class declaration

Tags:

python

class

Concepts of objects in python classes

While reading about old style and new style classes in Python , term object occurs many times. What is exactly an object? Is it a base class or simply an object or a parameter ?

for e.g. :

New style for creating a class in python

class Class_name(object):
pass

If object is just another class which is base class for Class_name (inheritance) then what will be termed as object in python ?

like image 502
Abhishek Avatar asked Apr 22 '26 05:04

Abhishek


1 Answers

From [Python 2.Docs]: Built-in Functions - class object (emphasis is mine):

Return a new featureless object. object is a base for all new style classes. It has the methods that are common to all instances of new style classes.

You could also check [Python]: New-style Classes (and referenced URLs) for more details.

>>> import sys
>>> sys.version
'2.7.10 (default, Mar  8 2016, 15:02:46) [MSC v.1600 64 bit (AMD64)]'
>>>
>>> class OldStyle():
...     pass
...
>>>
>>> class NewStyle(object):
...     pass
...
>>>
>>> dir(OldStyle)
['__doc__', '__module__']
>>>
>>> dir(NewStyle)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
 >>>
>>> old_style = OldStyle()
>>> new_style = NewStyle()
>>>
>>> type(old_style)
<type 'instance'>
>>>
>>> type(new_style)
<class '__main__.ClassNewStyle'>

In the above example, old_style and new_style are instances (or may be referred to as objects), so I guess the answer to your question is: depends on the context.

like image 139
CristiFati Avatar answered Apr 23 '26 18:04

CristiFati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!