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?

5
  • 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. Commented Nov 12, 2017 at 4:53
  • If possible you should begin with a newer Python version, at least 2.7 but much better 3.4 or later. Commented Nov 12, 2017 at 5:01
  • @MichaelButscher, regarding version: I was officially asked to study python from here. 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. Commented Nov 12, 2017 at 5:04
  • A wrote another answer about type that you might be interested in. Commented Nov 12, 2017 at 7:02
  • Also, there's this page in the Python docs that talks about objects and types, including bool. Commented Nov 12, 2017 at 7:05

1 Answer 1

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

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

6 Comments

did understand the answer but one big doubt remains, Data Types has no mention of object. so is object a datatype? or no?
@SrinathGanesh object is a data type and is similar to java.lang.Object.
@SrinathGanesh: object is the ultimate base class of everything in Python. Try isinstance(x, object) for any x.
No, the notation class type(object) doesn't mean it returns a class but that type is a class.
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).
|

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.