Typing Casting in Python Question [closed]

假装没事ソ 提交于 2019-12-08 05:16:12

问题


Im a AS3 developer, currently learning Python

In AS3 Id quite often do this:

for ( var foo in fooArray ) {
   trace(FooObject(foo).name);
}

Typing casting the objects in the array, so that I get code hinting in my IDE

How would I do this in Python?


回答1:


Types are determined at runtime in Python. Thus, there is typically less "code hinting" (I assume you mean completion, navigation, and so forth) in IDEs. There is still some.

Related: a commonly used IDE for Python development with some hinting is Eclipse (or Aptana) with pydev. Some installation instructions.




回答2:


There is no type casting in Python, as types are dynamic, so casting is completely pointless. Your IDE will give hints if it can figure out what type it is, which it often can't.




回答3:


Your best bet is to use logging. Python has a default logging module (with five strict levels: debug, info, error, etc), but I prefer my own tagalog (which supports n arbitrary tags on log messages).

With python logging module:

import logging

for foo in foo_list:
    logging.log(type(foo))

With tagalog:

import tagalog

for foo in foo_list:
    tagalog.log(type(foo))

Either of these approaches will write entries to a log. The output location for tagalog is always a file, which is specified in the 'log_file_path' variable here. The output location for Python's logging module (docs here) depends on your configuration.

To watch a file in realtime, do this in the linux/unix/mac terminal:

tail -f /path/to/file



回答4:


Figured this out, Python deal with classes a little more intelligently

In Actionscript

for ( var f in itemArray ) {
   // call function in f
   FooObject(f).doSomething()
}

In Python

for FooObject in itemArray: 
    # call function
    FooObject.foo()


来源:https://stackoverflow.com/questions/4726106/typing-casting-in-python-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!