刚刚开始关注Ruby元编程.mixin/modules总是让我困惑.
那么主要区别在于这还是潜伏着更大的龙? 例如
module ReusableModule
def module_method
puts "Module Method: Hi there!"
end
end
class ClassThatIncludes
include ReusableModule
end
class ClassThatExtends
extend ReusableModule
end
puts "Include"
ClassThatIncludes.new.module_method # "Module Method: Hi there!"
puts "Extend"
ClassThatExtends.module_method # "Module Method: Hi there!"
Run Code Online (Sandbox Code Playgroud) 我在我的系统上安装了一个python模块,我希望能够看到它中有哪些函数/类/方法.
我想在每一个上调用doc函数.在ruby中,我可以执行类似ClassName.methods的操作,以获取该类上可用的所有方法的列表.python中有类似的东西吗?
例如.就像是:
from somemodule import foo
print foo.methods # or whatever is the correct method to call
Run Code Online (Sandbox Code Playgroud) 由于最新的Rails 3版本不再自动加载来自lib的模块和类,加载它们的最佳方法是什么?
来自github:
Run Code Online (Sandbox Code Playgroud)A few changes were done in this commit: Do not autoload code in *lib* for applications (now you need to explicitly require them). This makes an application behave closer to an engine (code in lib is still autoloaded for plugins);
我一直在寻找互联网,没有明确的答案.
目前使用的NodeJS只CommonJS的语法来加载模块,如果你真的想使用标准ES2015模块语法,你要么必须事先transpile,或在运行时使用外部模块加载.
目前我不太喜欢使用这两种方法,NodeJS维护者是否计划支持ES2015模块?我还没有找到关于此的暗示.
目前NodeJS 6.x声称支持96%的ES2015功能,但没有任何模块参考(NodeJS ES2105支持链接).
你知道NodeJS是否会在不久的将来支持这些模块?
Node module.exports和ES6有什么区别export default?我试图找出当我尝试export default在Node.js 6.2.2中时为什么我得到"__不是构造函数"错误.
'use strict'
class SlimShady {
constructor(options) {
this._options = options
}
sayName() {
return 'My name is Slim Shady.'
}
}
// This works
module.exports = SlimShady
Run Code Online (Sandbox Code Playgroud)
'use strict'
class SlimShady {
constructor(options) {
this._options = options
}
sayName() {
return 'My name is Slim Shady.'
}
}
// This will cause the "SlimShady is not a constructor" error
// if in another file I try …Run Code Online (Sandbox Code Playgroud) 当我运行以下命令时:
sudo pip install python-ldap
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
在Modules/LDAPObject.c:9中包含的文件中:
Modules/errors.h:8:致命错误:lber.h:没有这样的文件或目录
任何想法如何解决这一问题?
我在使用Python 3安装软件包时遇到了麻烦.
我一直用setup.py install命令安装包.但是现在当我尝试安装ansicolors包时,我得到:
importerror"没有名为Setuptools的模块"
我不知道该怎么办,因为我从未有过Setuptools,而且我仍然安装了许多setup.py install没有setuptools的命令包,现在我应该得到setuptools.
我甚至无法安装setuptools,因为我有python 3.3和setuptools不支持python 3.
为什么我的安装命令不再起作用?
我想知道代码片段之间是否有任何区别
from urllib import request
Run Code Online (Sandbox Code Playgroud)
和片段
import urllib.request
Run Code Online (Sandbox Code Playgroud)
或者它们是否可以互换.如果它们是可互换的,那就是"标准"/"首选"语法(如果有的话)?
谢谢!
我正在Windows上学习nodejs.使用npm.cmd全局安装了几个模块,nodejs无法找到已安装的模块.以玉为例,
npm install jade -g
Run Code Online (Sandbox Code Playgroud)
Jade安装在目录中"C:\Program Files (x86)\nodejs\node_modules",但以下代码将失败并显示"Cannot find module 'jade'"错误,
var jade = require('jade');
Run Code Online (Sandbox Code Playgroud)
但是,当本地安装jade时,代码将成功运行(在npm中没有-g选项).我不想使用本地安装的模块,这对我来说是浪费磁盘空间,有人可以帮助我使全局安装的模块在Windows上运行吗?
根据http://www.faqs.org/docs/diveintopython/fileinfo_private.html:
像大多数语言一样,Python具有私有元素的概念:
- 私有函数,无法从模块外部调用
但是,如果我定义了两个文件:
#a.py
__num=1
Run Code Online (Sandbox Code Playgroud)
和:
#b.py
import a
print a.__num
Run Code Online (Sandbox Code Playgroud)
当我运行b.py它打印出来,1没有任何例外.diveintopython错了,还是我误解了什么?而且是有一些方法可以不定义模块的功能为私有?
module ×10
python ×5
node.js ×3
ecmascript-6 ×2
autoload ×1
class ×1
es6-modules ×1
extend ×1
function ×1
import ×1
include ×1
inspect ×1
javascript ×1
ldap ×1
npm ×1
package ×1
private ×1
python-3.3 ×1
reflection ×1
require ×1
ruby ×1
setup.py ×1
syntax ×1
windows ×1