来自Java背景,我明白__str__这就像是toString的Python版本(虽然我确实认识到Python是较旧的语言).
所以,我已经定义了一个小类以及__str__如下方法:
class Node:
def __init__(self, id):
self.id = id
self.neighbours = []
self.distance = 0
def __str__(self):
return str(self.id)
Run Code Online (Sandbox Code Playgroud)
然后我创建了一些它的实例:
uno = Node(1)
due = Node(2)
tri = Node(3)
qua = Node(4)
Run Code Online (Sandbox Code Playgroud)
现在,尝试打印其中一个对象时的预期行为是打印相关值.这也发生了.
print uno
Run Code Online (Sandbox Code Playgroud)
产量
1
Run Code Online (Sandbox Code Playgroud)
但是当我做以下事情时:
uno.neighbours.append([[due, 4], [tri, 5]])
Run Code Online (Sandbox Code Playgroud)
然后
print uno.neighbours
Run Code Online (Sandbox Code Playgroud)
我明白了
[[[<__main__.Node instance at 0x00000000023A6C48>, 4], [<__main__.Node instance at 0x00000000023A6D08>, 5]]]
Run Code Online (Sandbox Code Playgroud)
在哪里我期待
[[2, 4], [3, 5]]
Run Code Online (Sandbox Code Playgroud)
我错过了什么?还有什么其他令人讨厌的东西我在做什么?:)
有没有办法在VBS中列出已创建对象的可用方法?
例如:
Set IE = CreateObject("InternetExplorer.Application")
Run Code Online (Sandbox Code Playgroud)
我想列出此对象的可用属性,如下所示:
IE.AddressBar
IE.Application
IE.Busy
...
Run Code Online (Sandbox Code Playgroud)
或方法:
IE.ClientToWindow
IE.ExecWB
IE.GetProperty
...
Run Code Online (Sandbox Code Playgroud)
如何在VBS中发现任意有效对象的可用属性?
在Python 2.7中找出路径是否是套接字的最佳方法是什么?
os.path有... 目录,普通文件和链接的函数.该统计模块提供了一些S_IS ...功能,如S_ISSOCK(模式),这是我作为
import os, stat
path = "/path/to/socket"
mode = os.stat(path).st_mode
isSocket = stat.S_ISSOCK(mode)
print "%s is socket: %s" % (path, isSocket)
Run Code Online (Sandbox Code Playgroud)
这是首选方式吗?
我想将特定记录器名称的消息记录INFO到特定的日志处理程序(比如文件处理程序),同时将所有日志消息记录到控制台.Python是2.7版.
我到现在为止尝试创建两个记录器:
对于根记录器,我附加了一个logging.StreamHandler,并将日志级别设置为logging.DEBUG.
然后我将一个处理程序附加到命名记录器并logging.INFO为该记录器设置级别.
当我现在调用我使用命名记录器的模块时,我不再将DEBUG日志传播到根记录器了.
注意:extraLogger 在这里有一个StreamHandler来演示这个问题.在我的生产代码中,我使用了FileHandler
import logging
def do_logging(turn):
logger = logging.getLogger('extra')
logger.info('some info turn %d' % turn)
logger.debug('this is debug fudge turn %d' % turn)
rootLogger = logging.getLogger()
handler = logging.StreamHandler()
rootFormatter = logging.Formatter('root - %(levelname)s: %(msg)s')
handler.setFormatter(rootFormatter)
rootLogger.addHandler(handler)
rootLogger.setLevel(logging.DEBUG)
do_logging(1)
extraLogger = logging.getLogger('extra')
extraHandler = logging.StreamHandler()
extraFormatter = logging.Formatter('extra - %(levelname)s: %(msg)s')
extraHandler.setFormatter(extraFormatter)
extraLogger.addHandler(extraHandler)
extraLogger.setLevel(logging.INFO)
do_logging(2)
Run Code Online (Sandbox Code Playgroud)
实际产量:
root - INFO: some …Run Code Online (Sandbox Code Playgroud)