Srinath Ganesh
Srinath Ganesh

Reputation: 2558

How do you read documentation in python

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?

Upvotes: 2

Views: 575

Answers (1)

John Zwinck
John Zwinck

Reputation: 249193

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).

Upvotes: 2

Related Questions