我有一些代码:
baseTypes.ts
export namespace Living.Things {
export class Animal {
move() { /* ... */ }
}
export class Plant {
photosynthesize() { /* ... */ }
}
}
Run Code Online (Sandbox Code Playgroud)
dog.ts
import b = require('./baseTypes');
export namespace Living.Things {
// Error, can't find name 'Animal', ??
export class Dog extends Animal {
woof() { }
}
}
Run Code Online (Sandbox Code Playgroud)
tree.ts
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');
namespace Living.Things {
// Why do …Run Code Online (Sandbox Code Playgroud) 我想要实现的是创建一个包含多个函数的模块.
module.js:
module.exports = function(firstParam) { console.log("You did it"); },
module.exports = function(secondParam) { console.log("Yes you did it"); },
// This may contain more functions
Run Code Online (Sandbox Code Playgroud)
main.js:
var foo = require('module.js')(firstParam);
var bar = require('module.js')(secondParam);
Run Code Online (Sandbox Code Playgroud)
我firstParam遇到的问题是,它是一个对象类型,secondParam是一个URL字符串,但是当我有它时,它总是抱怨类型错误.
在这种情况下,如何声明多个module.exports?
我已经将一个项目导入到具有多个子项目的Android Studio中.
我想运行一个子项目.
我成功地将这个子项目的build.gradle作为一个模块.
为了运行它,我去了Run> edit configurations> +> Android Application.
问题:当我尝试选择模块时,没有显示在下拉列表中.
为什么是这样?
编辑:它显示为Groovy下的模块,但未在Android应用程序下显示.如何在Android应用程序下显示它?
有没有办法在模块内部设置全局变量?当我尝试以最明显的方式执行此操作时,如下所示,Python解释器说该变量__DBNAME__不存在.
...
__DBNAME__ = None
def initDB(name):
if not __DBNAME__:
__DBNAME__ = name
else:
raise RuntimeError("Database name has already been set.")
...
Run Code Online (Sandbox Code Playgroud)
并在将模块导入另一个文件后
...
import mymodule
mymodule.initDB('mydb.sqlite')
...
Run Code Online (Sandbox Code Playgroud)
追溯是: __DBNAME__
有任何想法吗?我正在尝试使用模块来设置单例,根据这个人的建议.
我想继承一个位于当前目录之上的文件中的类.
是否可以相对导入该文件?
我有一个声明了许多实例方法的模块
module UsefulThings
def get_file; ...
def delete_file; ...
def format_text(x); ...
end
Run Code Online (Sandbox Code Playgroud)
我想从一个类中调用其中的一些方法.你通常如何在ruby中这样做是这样的:
class UsefulWorker
include UsefulThings
def do_work
format_text("abc")
...
end
end
Run Code Online (Sandbox Code Playgroud)
include UsefulThings从中引入所有方法UsefulThings.在这种情况下,我只想要format_text并明确地不想get_file和delete_file.
我可以看到几个可能的解决方案:
Usefulthings并且只引入一些方法
UsefulThings在其中,然后委托format_text给该代理实例
为什么单个模块中有很多不相关的功能?它ApplicationHelper来自一个rails应用程序,我们的团队事实上决定将其作为任何其他地方不具备的特定任何东西的倾倒场.大多数独立的实用程序方法随处可见.我可以将它分解成单独的助手,但是它们中有30个,每个都有1个方法......这似乎没有效果
我需要做类似的事情:
if (condition) {
import something from 'something';
}
// ...
if (something) {
something.doStuff();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码没有编译; 它抛出SyntaxError: ... 'import' and 'export' may only appear at the top level.
我试着用System.import如图所示这里,但我不知道在哪里System的来了.是不是最终被接受的ES6提案?该文章中"programmatic API"的链接将我转储到已弃用的文档页面.
我试图从控制台运行一个模块.我的目录结构是这样的:
我正在尝试运行模块p_03_using_bisection_search.py,从problem_set_02目录使用:
$ python3 p_03_using_bisection_search.py
Run Code Online (Sandbox Code Playgroud)
里面的代码p_03_using_bisection_search.py是:
__author__ = 'm'
from .p_02_paying_debt_off_in_a_year import compute_balance_after
def compute_bounds(balance: float,
annual_interest_rate: float) -> (float, float):
# there is code here, but I have omitted it to save space
pass
def compute_lowest_payment(balance: float,
annual_interest_rate: float) -> float:
# there is code here, but I have omitted it to save space
pass
def main():
balance = eval(input('Enter the initial balance: '))
annual_interest_rate = eval(input('Enter the annual interest rate: '))
lowest_payment …Run Code Online (Sandbox Code Playgroud) 我想导入foo-bar.py.这有效:
foobar = __import__("foo-bar")
Run Code Online (Sandbox Code Playgroud)
这不是:
from "foo-bar" import *
Run Code Online (Sandbox Code Playgroud)
我的问题:有什么方法可以使用上面的格式,即from "foo-bar" import *导入一个包含其中的模块-?
module ×10
python ×4
javascript ×3
directory ×1
ecmascript-6 ×1
hyphen ×1
import ×1
livereload ×1
methods ×1
node.js ×1
python-3.6 ×1
ruby ×1
scope ×1
singleton ×1
typescript ×1
variables ×1
webpack ×1