Difference Between Type(Obj) and Obj._Class_

Difference between type(obj) and obj.__class__

type(obj) and type.__class__ do not behave the same for old style classes:

>>> class a(object):
... pass
...
>>> class b(a):
... pass
...
>>> class c:
... pass
...
>>> ai=a()
>>> bi=b()
>>> ci=c()
>>> type(ai) is ai.__class__
True
>>> type(bi) is bi.__class__
True
>>> type(ci) is ci.__class__
False

What is the difference between Type and Class?

The following answer is from Gof book (Design Patterns)

An object's class defines how the
object is implemented. The class
defines object's internal state and
the implementation of its
operations.

In contrast, an object's
type only refers to its interface - a
set of requests to which it can
respond.

An object can have many types,
and objects of different classes can
have the same type.

//example in c++
template<typename T>
const T & max(T const &a,T const &b)
{
return a>b?a:b; //> operator of the type is used for comparison
}

max function requires a type with operation > with its own type as one of it interface any class that satisfies the above requirement can be used to generate specific max<particular class/primitive type> function for that class.

Are classes necessary in Javascript? What effect do they have on creating objects?

Using new creates a new object whose internal prototype is the class's prototype. For example:

class Test {
say() {
console.log("I'm a test.");
}
}

let TestFromClass = new Test();
console.log(Object.getPrototypeOf(TestFromClass) === Test.prototype);

Why is `object` an instance of `type` and `type` an instance of `object`?

Answers to all your questions can be found in this book: Python Types and Objects

UPD: another link to the book. Let me know if it dies too.

The most important parts to answer your questions:

  • Has the type/class of an object also to be an object itself?

Yes, according to the Rule 1 from chapter 1:

"Everything is an object... Any classes that we define are objects, and of course, instances of those classes are objects as well."

  • Which one is the real base class object or type?

From chapter 2:

"These two objects are primitive objects in Python. We might as well have introduced them one at a time but that would lead to the chicken and egg problem - which to introduce first? These two objects are interdependent - they cannot stand on their own since they are defined in terms of each other."

Also Luciano Ramalho in his book "Fluent Python" says that this relation can't be expressed in Python (chapter 21):

"The classes object and type have a unique relationship: object is an
instance of type, and type is a subclass of object. This relationship
is "magic": it cannot be expressed in Python because either class would
have to exist before the other could be defined. The fact that type is
an instance of itself is also magical."

So, for your question:

  • How can a class (type) be an instance of itself?

Luciano says that it can't be expressed in Python too.

  • Is there a possibility to illustrate the relation between the object and the type class?

Many thanks to the author who made this illustration in сhapter 3:

relation between object, type and other classes

Python type() or __class__, == or is

For old-style classes, there is a difference:

>>> class X: pass
...
>>> type(X)
<type 'classobj'>
>>> X.__class__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: class X has no attribute '__class__'
>>> x = X()
>>> x.__class__
<class __main__.X at 0x171b5d50>
>>> type(x)
<type 'instance'>

The point of new-style classes was to unify class and type. Technically speaking, __class__ is the only solution that will work both for new and old-style class instances, but it will also throw an exception on old-style class objects themselves. You can call type() on any object, but not every object has __class__. Also, you can muck with __class__ in a way you can't muck with type().

>>> class Z(object):
... def __getattribute__(self, name):
... return "ham"
...
>>> z = Z()
>>> z.__class__
'ham'
>>> type(z)
<class '__main__.Z'>

Personally, I usually have an environment with new-style classes only, and as a matter of style prefer to use type() as I generally prefer built-in functions when they exist to using magic attributes. For example, I would also prefer bool(x) to x.__nonzero__().

The difference between Classes, Objects, and Instances

Java (and any other programming language) is modeled in terms of types and values. At the theoretical level, a value is a representation for some quantum of information, and a type is a set of values. When we say value X is an instance of type Y, we are simply saying that X is a member of the set of values that is the type Y.

So that's what the term "instance" really means: it describes a relationship not a thing.

The type system of the Java programming language supports two kinds of types, primitive types and reference types. The reference types are further divided into the classes and array types. A Java object is an instance of a reference type.

An object is a class instance or an array. (JLS 4.3.1)

That's the type theoretic view.

In practice, most Java developers treat the words "instance" and "object" as synonyms. (And that includes me then I'm trying to explain something quickly.) And most developers use the word "value" rather than "instance" to refer to an instance of a primitive type.

Difference between type and object primitive objects

Every class of objects in Python inherits the behaviours of the object class. Firstly, it means that

>>> isinstance(x, object)
True

will be true in Python 3 no matter what x happens to be bound to. It also means that everything will also inherit the methods of the object as such, unless overridden by a more specific class. Thus, x == y will resort to object.__eq__ if neither of x or y overrode the __eq__ method.

type in Python 3 is also an object - it inherits from object:

>>> isinstance(type, object)
True

It means that type is an object, and it has inherited the behaviour from the object class. That is what enables one to write things like

>>> type == type
True
>>> type.__eq__
<slot wrapper '__eq__' of 'object' objects>

The type.__eq__ was inherited from the object class.

But the object class itself is also a type:

>>> isinstance(object, type)
True

It means that the class object as an object inherits the behaviour of type - type is object's metaclass.

And of course type's metaclass is type:

>>> isinstance(type, type)
True

And the class object is an object too:

>>> isinstance(object, object)
True

When you construct an instance of class, the class itself is responsible for both the meat and bones for that class, but perhaps one could put the distinction in that the bones follows the ordinary class inheritance, and meat is split between the class and the metaclass lineage; and the metaclass is also the bones for the class object itself.



Related Topics



Leave a reply



Submit