Getting the class name of an instance in Python

How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived? Was thinking maybe the inspect module might have helped me out here, but it doesn't seem to give me what I want. And short of parsing the __class__ member, I'm not sure how to get at this informatio

在Python中获取实例的类名

我如何找到一个在Python中创建一个对象实例的类的名字,如果我这样做的函数是派生实例类的基类? 在想,也许检查模块可能会帮助我在这里,但它似乎并没有给我想要的。 并没有解析__class__成员,我不知道如何得到这些信息。 您是否尝试过__name__属性? 即type(x).__name__会给你类的名字,我认为你想要的是这个名字。 >>> import itertools >>> x = itertools.count(0) >>> type(x).__name__

How to get current time in Python?

什么是用于获得当前时间的模块/方法? >>> import datetime >>> datetime.datetime.now() datetime(2009, 1, 6, 15, 8, 24, 78915) And just the time: >>> datetime.datetime.time(datetime.datetime.now()) datetime.time(15, 8, 24, 78915) The same but slightly more compact: >>> datetime.datetime.now().time() See the documentation for more info. To save typing, you can

如何在Python中获取当前时间?

什么是用于获得当前时间的模块/方法? >>> import datetime >>> datetime.datetime.now() datetime(2009, 1, 6, 15, 8, 24, 78915) 而就在这个时候: >>> datetime.datetime.time(datetime.datetime.now()) datetime.time(15, 8, 24, 78915) 相同但稍微更紧凑: >>> datetime.datetime.now().time() 有关更多信息,请参阅文档 。 要保存输入,可以从datetime模块中导入datetime对象:

Is it possible to make Python etags a bit smarter with emacs?

I work on my Django project with emacs. In my virtualenv "postactivate" script I have the following simple command: find -L . -type f -name "*.py" | xargs etags -e > /dev/null 2>&1 & The TAGS file generates just fine but the system seems rather dumb. When the cursor is a model filter call, eg MyModel.objects.filter(...) and I hit M-., sometimes emacs takes me place w

emacs有可能使Python etags变得更智能一些吗?

我使用emacs工作在我的Django项目上。 在我的virtualenv“postactivate”脚本中,我有以下简单的命令: find -L . -type f -name "*.py" | xargs etags -e > /dev/null 2>&1 & 标签文件生成得很好,但系统似乎很笨拙。 当光标是模型过滤器调用时,例如 MyModel.objects.filter(...) 我打M-,有时候emacs会把我放在MyModel文件导入的地方(实际的导入语句)。 我只想访问类,方法和函数定义。 有没有办法让e

Parsing values from a JSON file?

I have this JSON in a file: { "maps": [ { "id": "blabla", "iscategorical": "0" }, { "id": "blabla", "iscategorical": "0" } ], "masks": [ "id": "valore" ], "om_points": "value", "parameters": [ "id": "valore" ] } I wrote this script which prints all of the json text: json_

从JSON文件解析值?

我有一个文件中的这个JSON: { "maps": [ { "id": "blabla", "iscategorical": "0" }, { "id": "blabla", "iscategorical": "0" } ], "masks": [ "id": "valore" ], "om_points": "value", "parameters": [ "id": "valore" ] } 我写了这个脚本来打印所有的json文本: json_data=open(file_dire

Getting same predicted values for all inputs in trained tensor flow network

I have created a tensorflow network designed to read data from this dataset (note: the information in this dataset is designed purely for test purposes and is not real): and am trying to build a tensorflow network designed to essentially predict values in the 'Exited' column. My network is structured to take 11 inputs, pass through 2 hidden layers (6 neurons each) with relu activation,

对经过训练的张量流网络中的所有输入获取相同的预测值

我创建了一个旨在从这个数据集中读取数据的tensorflow网络(注意:此数据集中的信息纯粹是为了测试目的而设计的,并非真实的): 我正试图构建一个旨在预测“已退出”列中的值的张量流网络。 我的网络结构为11个输入,经过2个隐含层(每个6个神经元),并激活relu,并使用sigmoid激活函数输出单个二进制值以产生概率分布。 我正在使用梯度下降优化器和均方误差成本函数。 然而,在训练了我的训练数据网络并预测了我的测试数据

Function speed improvement: Convert ints to list of 32bit ints

I'm looking for speedy alternatives to my function. the goal is to make a list of 32 bit integers based of any length integers. The length is explicitly given in a tuple of (value, bitlength). This is part of a bit-banging procedure for a asynchronous interface which takes 4 32 bit integers per bus transaction. All ints are unsigned, positive or zero, the length can vary between 0 and 20

功能提高速度:将整数转换为32位整数列表

我正在寻找快速替代我的功能。 目标是根据任意长度的整数制作32位整数列表。 长度明确地以(值,比特长度)的元组给出。 这是一个异步接口的一个bit-banging过程的一部分,每个总线事务处理需要4个32位整数。 所有整数都是无符号,正整数或零,长度可以在0到2000之间变化 我的输入是这些元组的列表,输出应该是隐式32位长度的整数,并且这些位按顺序排列。 其余的位不适合32也应该返回。 input: [(0,128),(1,12),(0,32)]

Why is [] faster than list()?

I recently compared the processing speeds of [] and list() and was surprised to discover that [] runs more than three times faster than list() . I ran the same test with {} and dict() and the results were practically identical: [] and {} both took around 0.128sec / million cycles, while list() and dict() took roughly 0.428sec / million cycles each. Why is this? Do [] and {} (and probably () a

为什么[]比list()更快?

我最近比较了[]和list()的处理速度,并惊讶地发现[]比list()运行速度快三倍以上。 我用{}和dict()进行了相同的测试,结果几乎相同: []和{}都花费了大约0.128秒/百万的周期,而list()和dict()花费了大约0.428秒/百万的周期。 为什么是这样? Do []和{} (也可能是()和'' )会立即传回一些空的库存文字的副本,而其明确命名的对应文件( list() , dict() , tuple() , str() )完全去创建一个对象,不管他们是否真

What's the correct way to check if an object is a typing.Generic?

I'm trying to write code that validates type hints, and in order to do so I have to find out what kind of object the annotation is. For example, consider this snippet that's supposed to tell the user what kind of value is expected: import typing typ = typing.Union[int, str] if issubclass(typ, typing.Union): print('value type should be one of', typ.__args__) elif issubclass(typ, ty

什么是正确的方法来检查一个对象是否是打字。通用?

我试图编写验证类型提示的代码,为了做到这一点,我必须找出注释是什么类型的对象。 例如,考虑这个片段应该告诉用户期望什么样的价值: import typing typ = typing.Union[int, str] if issubclass(typ, typing.Union): print('value type should be one of', typ.__args__) elif issubclass(typ, typing.Generic): print('value type should be a structure of', typ.__args__[0]) else: print('value type sho

Iterating over dictionaries using 'for' loops

I am a bit puzzled by the following code: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print key, 'corresponds to', d[key] What I don't understand is the key portion. How does Python recognize that it needs only to read the key from the dictionary? Is key a special word in Python? Or is it simply a variable? key is just a variable name. for key in d: will simply loop over the keys

使用'for'循环遍历字典

我对以下代码感到困惑: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print key, 'corresponds to', d[key] 我不明白的是key部分。 Python如何识别它只需要从字典中读取密钥? key是Python中的一个特殊词汇吗? 或者它只是一个变量? key只是一个变量名称。 for key in d: 将简单地遍历字典中的键,而不是键和值。 要循环键和值,可以使用以下命令: 对于Python 2.x: for key, value in d.iteritems(): 对

Understanding Python super() with

This question already has an answer here: What does 'super' do in Python? 5 answers super() lets you avoid referring to the base class explicitly, which can be nice. But the main advantage comes with multiple inheritance, where all sorts of fun stuff can happen. See the standard docs on super if you haven't already. Note that the syntax changed in Python 3.0: you can just say

通过。了解Python super()

这个问题在这里已经有了答案: “超级”在Python中做什么? 5个答案 super()可以避免显式引用基类,这很好。 但主要优点是多重继承,可以发生各种有趣的事情。 如果你还没有,请参阅super的标准文档。 请注意,在Python 3.0中更改了语法:您可以只说super().__init__()而不是super(ChildB, self).__init__()哪个IMO更好。 我想了解super() 我们使用super的原因是,可能使用协作多重继承的子类将在方法解析顺序(MRO)