How does Python interpreter look for types? [duplicate]

2024/11/20 16:34:18

If I write something like:

>>> a = float()

how does Python interpreter know where to look for type 'float'?

I know that 'float' is a variable defined in Lib/types.py and refers to built-in type types.FloatType. But how does the interpreter build a complete list of all possible types for a script (including user-defined and imported-module-defined)? Which places does it look in? And what do I do to build such a list inside a Python script?

Answer

Your question seems to be, "How is this a type declaration?" The answer is, it isn't a type declaration. Names in Python have no type associated with them. Names refer to values, and values have a type, determined at runtime.

When Python executes a = float(), it looks up the name float, and finds it in the builtins, it's a function. It calls that function with no arguments. The return value is a float object. The name a is then made to refer to that object. That's all it does. Before it's executed this line of code, Python has no idea what a will become, and it has no idea that floats will be involved.

Python is dynamic, so your line of code could have been in this program:

def float():return "I'm not a float!"a = float()

Now when a = float() is executed, the builtin has nothing to do with it, and there are no floats anywhere, and a refers to a string.

For more on names and values, see Facts and Myths about Python Names and Values.

https://en.xdnf.cn/q/118399.html

Related Q&A

title() method in python writing functions when word like arent

using functiondef make_cap(sentence):return sentence.title()tryining outmake_cap("hello world") Hello World# it workd but when I have world like "arent" and isnt". how to write…

Creating a C++ Qt Gui for a Python logic

I was presented with a Python logic for which I need to create a GUI. I want to use Qt for that purpose and ideally I would like to program it in C++, without using the Qt Creator.What are recommended …

Pythons BaseHTTPServer returns junky responses

I use Pythons BaseHTTPServer and implement the following very simple BaseHTTPRequestHandler:class WorkerHandler(BaseHTTPRequestHandler):def do_GET(self):self.wfile.write({"status" : "rea…

Why is matplotlib failing on import matplotlib.pyplot as plt

I installed matplotlib using conda:conda install matplotlibThe following code failed:#!/usr/bin/env python import matplotlib import matplotlib.pyplot as pltWith this error message:"ImportError: N…

Setting cell color of matplotlib table and save as a figure?

Im following this code a link! to save a table as the image, and I have some feature like check value in a cell then set color for a cell, but I added some code stylemap, it doesnt workimport pandas a…

Errno 111 Connection refused - Python Mininet API hosts client/server no connection?

I am new to Mininet and I am trying to find a way to use a script in python to execute a few tests using Mininet. More precisely I want to build topology and send a few xmlrpc request from one host t…

Finding the Corners of the an array of coordinates

I have a 2D array of Coordinates in Numpy.My goal is to attempt to find the corners (as if it were a square). So the :Top left: smallest x, highest y Top right: largest x, largest y bottom left: smalle…

How to make a dictionary retain its sort order?

def positive(self):total = {}final = {}for word in envir:for i in self.lst:if word in i:if word in total:total[word] += 1else:total[word] = 1final = sorted(total, reverse = True)return totalThis return…

Counting line frequencies and producing output files

With a textfile like this:a;b b;a c;d d;c e;a f;g h;b b;f b;f c;g a;b d;fHow can one read it, and produce two output text files: one keeping only the lines representing the most often occurring couple …

Check if parent dict is not empty and retrieve the value of the nested dict

Lets suppose that I have a nested dictionary which looks like that:parent_dict = { parent_key: {child_key: child_value}How can I write the following code:if parent_dict.get(parent_key) is not None and …