可能重复:
在C中,main()方法最初是如何调用的?
我想知道,谁在C中调用主函数.主函数的
实际用途是什么(为什么main是特殊/必要的)?
我可以用主函数编写ac程序吗?
我正在尝试使用pygame-clients运行一个扭曲的服务器:
class ChatClientProtocol(LineReceiver):
def lineReceived(self,line):
print (line)
class ChatClient(ClientFactory):
def __init__(self):
self.protocol = ChatClientProtocol
def main():
flag = 0
default_screen()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
pos = pygame.mouse.get_pos()
# some rect.collidepoint(pos) rest of loop...
Run Code Online (Sandbox Code Playgroud)
这是服务器:
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class Chat(LineReceiver):
def __init__(self, users, players):
self.users = users
self.name …Run Code Online (Sandbox Code Playgroud) 我使用以下钩子检查正在执行包含的模块include Foo:
module Foo
def self.included(includer)
puts includer
end
end
Run Code Online (Sandbox Code Playgroud)
Module#include在模块中(通常使用它)与顶层的行为不同.在模块内部,self是模块,它是一个实例Module.当我打电话include时,模块做,包括是什么样self的:
module Bar
puts self # => Bar
include Foo # => includer: Bar
end
Run Code Online (Sandbox Code Playgroud)
在ruby脚本的顶层,self是main,它是一个实例Object.当我把include在顶层,该模块包括做是Object,之类的东西self是:
puts self # => main
include Foo # => includer: Object
Run Code Online (Sandbox Code Playgroud)
有人可以解释原因吗?
顶级对象必须是特殊的; 如果我打电话to_s或打电话inspect,它只是说main,但如果我创建另一个对象Object.new并调用to_s或inspect打开它,我得到通常的对象表示法:#<Object:0x007fae0a87ac48> …
当我使用该命令运行代码时python filename.py,我收到以下错误,
/Library/anaconda/bin/python: can't find '__main__' module in filename.py
我不确定这里到底出了什么问题.我需要帮助纠正这个问题.我怎么能纠正这个?
SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']}
def approximate_size(size, a_kilobyte_is_1024_bytes=True):
'''Convert a file size to human-readable form.
Keyword arguments:
size -- file size in bytes
a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024
if False, use multiples of 1000
Returns: string
'''
if size < 0:
raise ValueError('number must be non-negative')
multiple = 1024 …Run Code Online (Sandbox Code Playgroud) 我们有一个使用异步初始化过程的 winforms 应用程序。简单地说,应用程序将运行以下步骤:
当前现有的和工作的代码如下所示:
[STAThread]
private static void Main()
{
SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext());
var task = StartUp();
HandleException(task);
Application.Run();
}
private static async Task StartUp()
{
await InitAsync();
var frm = new Form();
frm.Closed += (_, __) => Application.ExitThread();
frm.Show();
}
private static async Task InitAsync()
{
// the real content doesn't matter
await Task.Delay(1000);
}
private static async void HandleException(Task task)
{
try
{
await Task.Yield();
await task;
}
catch (Exception e)
{
Console.WriteLine(e);
Application.ExitThread(); …Run Code Online (Sandbox Code Playgroud) 函数名是否应该main()为空并且在函数本身内调用参数,或者将它们作为函数的输入是否可接受main(arg1, arg2, arg3)?
我知道它有效,但我想知道它是不是很糟糕的编程习惯.如果这是重复的道歉,但我看不出专门针对Python的问题.
Imagine that you have a module X whose functionalities are available to the user through a Command Line Interface. Such module doesn't do much in and of itself but it allows for other people to create plugin-like modules they can hook-up to module X. Ideally, the plugins could be used through X's CLI.
Thus my question:
What do you need to do in order to hook-up whatever functionality
a plugin might provide to X's CLI?
This …
我倾向于问这个问题,因为大多数时候我们直接调用 runApp 函数 main 而不做任何其他事情。我的问题是为什么 runApp 和 main 保持不同?保留 main 函数或 runApp 函数而丢弃其他函数可能很简单?
https://martimm.github.io/gnome-gtk3/content-docs/tutorial/Application/sceleton.html,缩写:
在 Raku 中,主程序保持小是很重要的。这是因为所有代码、程序和模块都被解析并编译成将由虚拟机执行的中间代码。大多数时候是 MoarVM,但也有一个 JVM,之后可能还有其他的。无论如何,在运行之前,已编译的模块会保存到 .precomp 目录中,但不会保存到程序中。这意味着程序总是在运行之前被解析和编译,这就是保持它小的原因。
Run Code Online (Sandbox Code Playgroud)use UserAppClass; my UserAppClass $user-app .= new; exit($user-app.run);好吧,你不能比这更小……,或者可以使用这种单衬;
exit(UserAppClass.new.run).其余代码在 UserAppClass 中定义。
非常好。
现在,我们的程序需要接受参数。sub MAIN解析参数并生成$*USAGE免费的,所以我们将利用sub MAIN.
我们将d by programsub MAIN放入used 中,但我们得到的程序不知道参数。并且在模块中时不执行。
我们把入程序,以便它理解论点,但它已经不小了。.raku.rakumod.rakusub MAINsub MAIN.raku
此外,嵌入POD的程序很可能会驻留在.raku程序中。
放入POD一个used by.raku程序.rakumod,我们得到了POD一些隐藏的东西。
将POD进入.raku程序并再次它已经不小了。
另外,这种方法是否有任何命名约定?
说,你有一个程序Report when your coffee is …
optimization program-entry-point command-line-arguments precompile raku
我有一个像这样的 setup.py :
#!/usr/bin/env python
from setuptools import setup, find_packages
setup(
name="myproject",
package_dir={"": "src"},
packages=find_packages("src"),
entry_points={
"console_scripts": [
"my-script = myproject.myscript:entrypoint",
],
},
)
Run Code Online (Sandbox Code Playgroud)
如何entry_points使用 setuptools 在 pyproject.toml 中编写该配置?
我猜测类似这样的事情,继续setuptools 的 pyproject.toml 文档,它说我需要在引用入口点的文档之后使用“INI 格式” ,但它似乎没有给出示例,我的猜测关于如何将 setuptools 语法与 pyproject.toml 语法结合起来是错误的(我从pip install -e .该报告中得到了回溯pip._vendor.tomli.TOMLDecodeError: Invalid value,指向entry-pointspyproject.toml 中的行):
[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"
[metadata]
name = "myproject"
[tool.setuptools]
package-dir = {"" = "src"}
[tool.setuptools.packages.find]
where = ["src"]
[tool.setuptools.dynamic]
entry-points …Run Code Online (Sandbox Code Playgroud) python program-entry-point setuptools python-packaging pyproject.toml
python ×4
raku ×2
async-await ×1
c ×1
c#-7.1 ×1
class ×1
flutter ×1
include ×1
linux ×1
optimization ×1
perl6 ×1
precompile ×1
pygame ×1
ruby ×1
run-app ×1
setuptools ×1
subroutine ×1
twisted ×1
winforms ×1