开发者

Typing Casting in Python Question [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomp开发者_Go百科lete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

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?


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.


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.


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


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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜