1

Can someone explain why the x below can act as a function float()? Basically I dont understand what means? is this a internal function or implicit object ?

>>> x=type(0.0)
>>> x
<type 'float'>
>>> x('9.823')
9.823
2
  • 1
    Because this is same as doing x = float. Commented Mar 1, 2016 at 15:00
  • 1
    Guessing it's the same as doing x = float and then calling x('0.823'). But never the less interesting, never thought of using the type object in junction with a function call : ) Commented Mar 1, 2016 at 15:00

3 Answers 3

1

It's exactly the same as writing float('9.823'). In fact, you can easily see that as follows:

>>> type(0.0) is float
True
>>> 

And you can use them in exactly the same way:

>>> float('9.823')
9.823
>>> type(0.0)('9.823')
9.823
>>> 

It's just invokes the constructor for the float type.

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

2 Comments

so <type 'float'> is a constructor?
Well, it's a type that has a constructor, just as a class is a type that has a constructor (with the same syntax).
1

You're setting the variable x to the type float. The command type() returns the type of whatever is inside the brackets. In your case, you provided the type command with a float and setting that return of float to your variable x.

2 Comments

I guess my question was what the operator <> means?
That is just how a type displays when you print it. Generally you don't use <> brackets in Python.
0

It can act as the function float because you are effectively making x = float.

As an example, you could also, for instance do this:

x = type(1) #int
print x(1.1111) # will print 1

Comments

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.