我剛剛在Python中約定了self約定,並開始製作更復雜的代碼。然而,一位經驗豐富的程序員和我的朋友告訴我,對類方法中的每個變量使用self都是浪費。 據我所知,self將導致該變量歸因於該類。因此,除非有必要,否則避免使用self是好習慣嗎? 下面是一些代碼,從API獲取英雄聯盟信息,並將每個變量存儲在self.var_name中,以說明我是如何(可能不必要地)使用self。 async de
我的第一個類有不變。我想讓它以某種方式作爲動態值工作。 class Post < ActiveRecord::Base
TEST = ["#{self.name}", "test1"]
end
class FakePost < Post
end
在rails console我試圖用FakePost訪問TEST不變,但它仍然顯示self爲「郵報」的對象。有什麼辦法可以做到這一
我瞭解到,靜態優於自因爲自做後期靜態綁定。 但我不知道這將是最好的參考常量變量。 class Black
{
const color = 'black';
public function byThis()
{
return $this::color;
}
public function bySelf()
{
我試圖按所有者過濾查詢集,但出於某種原因使用self.request.user結果爲NameError。 class PackageListView(LoginRequiredMixin, ListView, Self):
model = Package
user = self.request.user
queryset = Package.objects.filte
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/types.h>
int main(){
int systid=syscall(186);
int pt_tid=pthread_self();
pid_t id=getpid();
printf("pid=%d
在下面的代碼中,我對「自我」實際引用的內容感到困惑。 let imageURL = URL(string : 「http://exampleURLh.jpg」)!
let task=imageDataURLSession.sharedSession().dataTaskWithURL(imageURL, {(data, response, error) in
print (「task