Object parameter in python class declaration

Question:

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 ?

Asked By: Abhishek

||

Answers:

Object is a generic term. It could be a class, a string, or any type. (and probably many other things)

As an example look at the term OOP, “Object oriented programming”. Object has the same meaning here.

Answered By: mvrak

All objects in python are ultimately derived from “object”. You don’t need to be explicit about it in python 3, but it’s common to explicitly derive from object.

Answered By: user11584987

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.

Answered By: CristiFati

An Object is something in any Object Oriented language including Python, C#, Java and even JavaScript these days. Objects are also prevalent in 3d modeling software but are not per se the same thing.

An Object is an INSTANTIATED class. A class is a blueprint for creating object. Almost everything in Python is an object.

In reference to how you seem to be regarding to exclusivity of objects in python, yes, there is an object class. C# has them too and possibly others where you would use it not dissimilar to creating a string or int.

Answered By: grendeld
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.