Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meant by "classes themselves are objects"?

I was just reading the Python documentation about classes; it says, in Python "classes themselves are objects". How is that different from classes in C#, Java, Ruby, or Smalltalk? What advantages and disadvantages does this type of classes have compared with those other languages?

like image 214
srinivas reddy thatiparthy Avatar asked Jun 26 '11 04:06

srinivas reddy thatiparthy


People also ask

What is mean by classes and object?

A class is a user-defined type that describes what a certain type of object will look like. A class description consists of a declaration and a definition. Usually these pieces are split into separate files. An object is a single instance of a class. You can create many objects from the same class type.

Are classes considered objects?

A class isn't an object, you can think of it as a 'blueprint' for an object. It describes the shape and behaviour of that object. Objects are instances of a class.

Can a class have its own object?

Because in Java, a variable of type abc doesn't contain an abc object. A variable of type abc contains a reference to an abc object. Your reasoning would be valid in say C++. But a class can have static object of self type.

What are classes and objects in Python?

An object is simply a collection of data (variables) and methods (functions) that act on those data. Similarly, a class is a blueprint for that object. We can think of a class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc.


1 Answers

In Python, classes are objects in the sense that you can assign them to variables, pass them to functions, etc. just like any other objects. For example

>>> t = type(10)
>>> t
<type 'int'>
>>> len(t.__dict__)
55
>>> t() # construct an int
0
>>> t(10)
10

Java has Class objects which provide some information about a class, but you can't use them in place of explicit class names. They aren't really classes, just class information structures.

Class C = x.getClass();
new C(); // won't work
like image 161
Daniel Lubarov Avatar answered Oct 21 '22 07:10

Daniel Lubarov