2

Headsup: I have started with python 2.6 about 2 hours ago. Been doing Java, C, etc. till now

TL;DR

In Java I want to understand what is an Object, I look at the javadoc here Where do I find a similar clear documentation of what a function does in python?


Long story

I understood the following

  1. A variable 'a' is not restricted to a given datatype.
  2. A variable 'a' can hold an 'int' and a 'float' at different points in time.

Ended up with a simple code and out of curiosity checked up type()

a = 1     # type(a) is int
a = 1.2   # type(a) is float
a = 1     # type(a) is int

Wanted to understand what type() in python really does and found type function that reads 'class type(object)' but Built-in data-types has no mention of either 'class' or 'object'

when i read 'class type(object)' I interpret it as

  1. there is a function called 'type'
  2. this accepts an object as a parameter
  3. this returns a class

But python documentation is contradicting saying "return the type of an object. The return value is a type object." and the code snippet at the documentation seemed to be creation of a class which made no sense either.

a = False # type(a) returns 'bool'

Built-in data-types talks about Boolean, so where is bool documentation located?

Srinath Ganesh
  • 2,496
  • 2
  • 30
  • 60
  • `type` is itself a class (similar to Java's `java.lang.Class`). For each type (which may be a class inside Python or a type from a compiled binary extension) there exists an object of type `type`. – Michael Butscher Nov 12 '17 at 04:53
  • If possible you should begin with a newer Python version, at least 2.7 but much better 3.4 or later. – Michael Butscher Nov 12 '17 at 05:01
  • @MichaelButscher, regarding version: I was officially asked to study python from [here](https://developers.google.com/edu/python/introduction). the tutorial recommended me **While we recommend "avoiding" Python 3 for now, recognize that it is the future, as all new features are only going there.** – Srinath Ganesh Nov 12 '17 at 05:04
  • A wrote [another answer](https://stackoverflow.com/a/47007160/8651755) about `type` that you might be interested in. –  Nov 12 '17 at 07:02
  • Also, there's [this page](https://docs.python.org/3.6/reference/datamodel.html#objects-values-and-types) in the Python docs that talks about objects and types, including `bool`. –  Nov 12 '17 at 07:05

1 Answers1

2

Wanted to understand what type() in python really does

In Python, everything is an object. So when you see this:

class type(object)

It is telling you that it accepts an object (generically) and returns a "class" which is also an object. A class in Python is an object which describes other objects--a "meta object" if you prefer. This is by contrast to e.g. C++, where a class is not an object at all (it cannot be stored).

In Python, types are objects, so for example type(type(type('hello'))) gives you type (because the result of the type() function is always a type object).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • did understand the answer but one big doubt remains, [Data Types](https://en.wikibooks.org/wiki/Python_Programming/Data_Types#Built-in_Data_types) has no mention of object. so is object a datatype? or no? – Srinath Ganesh Nov 12 '17 at 04:55
  • @SrinathGanesh `object` is a data type and is similar to `java.lang.Object`. – Michael Butscher Nov 12 '17 at 04:59
  • @SrinathGanesh: `object` is the ultimate base class of everything in Python. Try `isinstance(x, object)` for any x. – John Zwinck Nov 12 '17 at 05:03
  • No, the notation class type(object) doesn't mean it returns a class but that type **is** a class. – Michael Butscher Nov 12 '17 at 05:05
  • Ok **object** is a *data type*. Java Object is documented [here](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html), Where is Python object documented at?? None at [data types](https://en.wikibooks.org/wiki/Python_Programming/Data_Types#Built-in_Data_types) – Srinath Ganesh Nov 12 '17 at 05:06
  • 2
    `type` is a class that is the root class of all classes; `type` is _also_ an object. `type(x)` _always_ returns a type (AKA class) and the returned type/class is _also_ an object. You can verify this with `isinstance(type, object)` and `isinstance(type(x), object)`. –  Nov 12 '17 at 07:09