我正在尝试使用泛型__init__值创建一个类,但其子类具有默认值,如下所示:
class Enemy:
def __init__(self, difficulty, power, MaxHP, magic, MaxMP, speed, name):
self.power = power + 2*difficulty
self.HP = self.MaxHP = MaxHP + 5*difficulty
self.magic = magic + 2* difficulty
self.MP = self.MaxMP = MaxMP + 5*difficulty
class Goblin(Enemy):
def __init_(self, difficulty = 1, power = 1, MaxHP = 5, magic = 1, MaxMP = 5, speed = 5, name = "Goblin"):
super(Goblin, self).__init__(self, power, MaxHP, magic, MaxMP, speed, name)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试创建一个没有全部默认值的 Goblin 对象时(例如,我将只输入一个难度值),它告诉我我需要完整的 8 个参数,即使其余参数已给出默认值。有什么理由我不能这样做还是我在这里做错了什么?
是否可以使用 init rc 脚本在 android 源代码中创建空文件,就像我们创建文件夹一样。mkdir /tmp 创建文件夹,但 touch /tmp/test 不执行任何操作。
请帮忙
在学习 Swift 时,初始化类实例似乎有两种方法:
// Approach A
class Person {
let first: String = "bob"
let last: String = "barker"
}
let worker = Person()
worker.first
worker.last
// Approach B
class Person2 {
let first2: String
let last2: String
init() {
self.first2 = "bill"
self.last2 = "williams"
}
}
let dealer = Person2()
dealer.first2
dealer.last2
Run Code Online (Sandbox Code Playgroud)
我有什么理由使用一种方法而不是另一种方法?
我正在用 Y86 编写一个程序,但我在设置堆栈和基指针的代码行中不断收到错误“找不到标签”。我的代码是:
.pos 0
init:
irmovl Stack, %esp //Set up stack pointer
irmovl Stack, %ebp //Set up base pointer
call main //call main program
halt //Terminate program
Run Code Online (Sandbox Code Playgroud)
这就是我的笔记中的显示方式,但是当我尝试编译时,我得到了
Error on line 8: Can't find label
Line 8, Byte 0x0006: irmovl stack, %esp //Set up stack pointer
Error on line 9: Can't find label
Line 9, Byte 0x000c: irmovl stack, %ebp //Set up base pointer
Run Code Online (Sandbox Code Playgroud)
我尝试将 .pos 0 行放入 init 函数中(我认为这可能会有所帮助),并简单地取消 init: 行,但我仍然遇到同样的问题。
我正在关注Swift & the Objective-C Runtime,它适用于普通方法。
我喜欢 swizzle init 方法,根据我的理解,init 就像一个类方法。所以我尝试将 init 混合为实例和类方法。但它似乎不起作用
我可以使用 Objective C 让它工作,只是想知道如何让它在 Swift 中工作
摘自我的要点
dispatch_once(&Static.token) {
let originalSelector = Selector("init:source:destination:")
let swizzledSelector = Selector("ftg_init:source:destination:")
let originalMethod = class_getClassMethod(self, originalSelector)
let swizzledMethod = class_getClassMethod(self, swizzledSelector)
let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
Run Code Online (Sandbox Code Playgroud) 我构建了一个shell脚本,用于inotifywait自动检测特定目录上的文件更改。当一个新的 PDF 文件被放到目录中时,这个脚本应该关闭,然后它应该触发ocropus-parser在它上面执行一些命令。编码:
#!/bin/sh
inotifywait -m ~/Desktop/PdfFolder -e create -e moved_to |
while read path action file; do
#echo "The file '$file' appeared in directory '$path' via '$action'"
# Check if the file is a PDF or another file type.
if [ $(head -c 4 "$file") = "%PDF" ]; then
echo "PDF found - filename: " + $file
python ocropus-parser.py $file
else
echo "NOT A PDF!"
fi
done
Run Code Online (Sandbox Code Playgroud)
当我通过终端运行此脚本时,这很有效,./filenotifier.sh但是当我重新启动我的Linux(Ubuntu 14.04)时,我的 …
我有一些具有.py以下结构的文件夹和文件:
parent/
__init__.py
test.ipynb
code/
__init__.py
common.py
subcode/
__init__.py
get_data.py
Run Code Online (Sandbox Code Playgroud)
在__init__该parent文件夹下的文件中,我有import code,在其中之一中code,我有import subcode。但是当我尝试时import code.subcode,我得到了这样的错误:
ImportError: No module named 'code.subcode'; 'code' is not a package
Run Code Online (Sandbox Code Playgroud)
但是当我刚刚时import code,没有抛出任何错误。但是,当我调用 时code.subcode,会发生此错误:
AttributeError: module 'code' has no attribute 'subcode'
Run Code Online (Sandbox Code Playgroud)
test.ipynb我在位于目录根目录的中尝试了上面提到的所有内容。
您知道原因是什么以及如何解决吗?谢谢!
我最近遇到了一种情况,即使我使用 init 块为构造函数分配了一个值,我的标准变量的值也会被默认值替换。
我尝试的是:
class Example(function: Example.() -> Unit) {
init {
function()
}
var name = "default name"
}
// assigning it like this:
val example = Example { name = "new name" }
// print value
print(example.name) // prints "default name"
Run Code Online (Sandbox Code Playgroud)
经过一番挣扎,我发现 init 块的位置很重要。如果我将 init 块放在类的最后一个,它首先使用默认名称初始化名称,然后调用 function() 将值替换为“新名称”。
如果我把它放在第一位,它没有找到名称,并且在初始化属性时将其替换为“默认名称”。
这对我来说很奇怪。谁能解释为什么会这样?
假设我有这个:
class Test():
def __init__(self, number):
self.number = number
await self.TestPrint()
async def TestPrint(self):
print(self.number)
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这不起作用,因为__init__不是async,而且我无法调用await该函数。
我希望能够在假设我想维护这个函数 async 的情况TestPrint下运行。__init__
我还希望它与类之外的任何其他内容(其他函数、其他类、main 等)无关。
抱歉举了这个毫无意义的例子。试图简化它,仍然不明白\xe2\x80\x99s发生了什么。以下给了我Variable \'self.greeting\' used before being initialized:
struct MyView: View {\n @State var greeting: String\n var length = Measurement<UnitLength>(value: 5, unit: UnitLength.miles)\n init() {\n greeting = "Hello, World!" // Variable \'self.greeting\' used before being initialized\n }\n var body: some View {\n Text(greeting)\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n虽然这工作正常(var length不同;否则相同):
struct MyView: View {\n @State var greeting: String\n var length = 5 // now Int\n init() {\n greeting = "Hello, World!"\n }\n var body: some View {\n Text(greeting)\n }\n}\n …Run Code Online (Sandbox Code Playgroud)