3

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 ?

1
  • 2
    object is a class (a type) in Python. In python 3, the type hierarchy has been unified (i.e. there are only new-style classes) so everything is an object, i.e. it is always true that isinstance(x, object) for any x, just as it is true that isinstance(42, int), and both object and int are class objects. Commented May 31, 2019 at 22:27

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

old_style and new_style are variables right? (or instance variables )
Yes they are. But as I said the term "object" is also used to reference them. Anyway (to keep things simple) in the current context, object is the root node in the class hierarchy.
So, simply it's just context that will decide definition of this object term . Otherwise it's generic in OOPs and definition applicable anywhere regardless any language
@LuciFiEr they are not instance variables.
Regarding my previous comment: they are variables, but they are not instance variables (or attributes, or fields). Check docs.python.org/2/tutorial/classes.html#instance-objects.
0

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.

Comments

0

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.

5 Comments

Is this object is base class for all classes just like we have in java?
So, here in my example object refers to that object class which generates an object , whereas if suppose i write : dog = Dog() then Dog () is object in techincal terms
I think I see where the confusion is. Most languages will have an object class that is for like passing an empty object or creating a temporary object or some other thing . They behave somewhat the same way but they're not to be confused with general OOP usage of objects and classes
that's a class you're making thats passing an object look @CristiFati s answer
"An Object is an INSTANTIATED class. A class is a blueprint for creating object. Almost everything in Python is an object." In python, a class is merely another object, everything is an object, and classes have their own class, a metaclass, type (for new-style classes, or always in python 3)
-1

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.

1 Comment

In context to python ,Object used in class declaration can be anything ? Why classes are different in python ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.