是否可以在python中声明函数并在以后或在单独的文件中定义它们?
我有一些代码:
class tata:
def method1(self):
def func1():
# This local function will be only used in method1, so there is no use to
# define it outside.
# Some code for func1.
# Some code for method1.
Run Code Online (Sandbox Code Playgroud)
问题是代码变得混乱且难以阅读.所以我想知道是否有可能在func1内部声明method1并在以后定义它?
我正在尝试建立一个模型,根据某些组件是否存在(在我的情况下,流体端口),这些模型的方程略有不同.
像下面这样的代码不起作用:
parameter Boolean use_component=false;
Component component if use_component;
equation
if use_component then
component.x = 0;
end if;
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
我正在从谷歌为webgl-utils.js创建一个d.ts文件
我有一个问题,其中一个最后一行,全局对象中的方法是'猴子修补'(我认为这是正确的术语)
问题行如下:
/**
* Provides requestAnimationFrame in a cross browser way.
*/
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
window.setTimeout(callback, 1000/60);
};
})();
Run Code Online (Sandbox Code Playgroud)
我如何在我的打字稿文件中声明这一点,以便在使用该函数时不会出现编译错误:
function tick()
{
requestAnimFrame(tick);
drawScene();
}
Run Code Online (Sandbox Code Playgroud)
我现在尝试过:
interface window
{
requestAnimFrame(): any;
}
Run Code Online (Sandbox Code Playgroud)
但这不会消除错误:
The name 'requestAnimFrame' does not exist in the current scope
Run Code Online (Sandbox Code Playgroud) 此代码将编译并在当前 C 标准下定义良好:
static int foo(int);
extern int foo(int);
Run Code Online (Sandbox Code Playgroud)
该标准规定,在这种情况下(C11:6.2.2 标识符的链接(p4)):
对于在该标识符的先前声明可见的范围内使用存储类说明符 extern 声明的标识符,31) 如果先前声明指定内部或外部链接,则后面声明中标识符的链接与先前声明中指定的链接。[...]
...这意味着该int foo(int)函数已声明static int foo(int)。
像这样交换这些声明:
extern int foo(int);
static int foo(int);
Run Code Online (Sandbox Code Playgroud)
...使用 GNU GCC 给我一个编译器错误:
'foo' 的静态声明遵循非静态声明
我的问题是:第二种情况是一个错误并且没有以与第一种情况类似的方式处理的设计原理是什么?我怀疑这与单独的翻译单元更易于管理和#include? 我觉得如果不理解这一点,我可以在未来的 C 项目中为一些错误敞开心扉。
我正在使用以下标志(其中cc是gcc 4.2或clang 8.0):
$ cc -Wall -Werror -pedantic -ansi -std=c89 main.c
(我知道-ansi标志在这种情况下有点多余)
以下给了我预期的错误
main.c:31:8: warning: ISO C90 forbids mixing declarations and code [-Wdeclaration-after-statement]
vec3 abc = {0};
Run Code Online (Sandbox Code Playgroud)
int main()
{
vec3 a = {0};
vec3 b = {0};
Vec3(2, 2, 2);
vec3 abc = {0}; // Declared after a function call
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,以下内容不
int main()
{
vec3 a = Vec3(0, 1, 2);
vec3 b = Vec3(0, 1, 2);
vec3 abc = {0}; // Declared after a function call …Run Code Online (Sandbox Code Playgroud) 考虑下面的两个翻译单元。我的目标是内联函数addinfile1.c和main.c.
文件1.c:
extern inline int add(int x, int y)
{
return x + y;
}
void g(void)
{
add(1, 1); // this will probably be inlined
}
Run Code Online (Sandbox Code Playgroud)
主文件:
extern int add(int, int);
void f(void)
{
add(2, 4); // this doesn't appear to be inlined
}
int main(void)
{
f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
生成的程序集表明 add(2, 4) 未内联。为什么?或者是它的规则,对于在翻译单元内联函数,因此它必须被内嵌在翻译单元中声明,因此其定义必须被视为?
当我编译下面的代码时有一个奇怪的行为:
class Foo {
};
int main() {
Foo(b);
}
Run Code Online (Sandbox Code Playgroud)
即使没有b. 对此有什么解释吗?
任何人都可以向我解释为什么结果是 2,x 正在使用哪个以及为什么。
auto x = 0;
int f(int i){
auto x = 1;
{
static auto x = 0;
x += i;
}
return x;
}
int main() {
cout << f(1) + f(2) <<endl;// result 2
return 0;
}
Run Code Online (Sandbox Code Playgroud) 该tsc编译器总是高高兴兴地编译console.log("foo")。变量在哪里console声明?即使lib在我的tsconfig.json. 所以console假设是通用的?总是声明哪些其他变量?更一般地说,我怎样才能找出存在的其他声明,以及它们来自哪里?(我能找到的唯一调试标志是--extendedDiagnostics,但它似乎几乎没用。)
我知道像这样的声明
int* a[5];
Run Code Online (Sandbox Code Playgroud)
声明a为一个包含 5 个指针的数组int。但是我如何仅通过使用 C++ 标准来推断出这一点?
declaration ×10
c ×3
c++ ×3
typescript ×2
arrays ×1
c89 ×1
conditional ×1
extern ×1
function ×1
grammar ×1
inline ×1
javascript ×1
linkage ×1
modelica ×1
python ×1
scope ×1
statements ×1
static ×1
syntax ×1