Python getattr() လုပ်ဆောင်ချက်
ဥပမာ
"ပုဂ္ဂိုလ်" အရာဝတ္ထု၏ "အသက်" ပိုင်ဆိုင်မှုတန်ဖိုးကို ရယူပါ-
class Person:
name = "John"
age = 36
country = "Norway"
x =
getattr(Person, 'age')
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
လုပ်ဆောင်ချက် သည် getattr()
သတ်မှတ်ထားသော အရာဝတ္ထုမှ သတ်မှတ်ထားသော အရည်အချင်းတန်ဖိုးကို ပြန်ပေးသည်။
အထားအသို
getattr(object, attribute, default)
ကန့်သတ်တန်ဖိုးများ
Parameter | Description |
---|---|
object | Required. An object. |
attribute | The name of the attribute you want to get the value from |
default | Optional. The value to return if the attribute does not exist |
နောက်ထပ် ဥပမာများ
ဥပမာ
ရည်ညွှန်းချက်မရှိသည့်အခါ စာတစ်စောင်ရေးရန် "မူလ" ဘောင်ကို အသုံးပြုပါ-
class Person:
name = "John"
age = 36
country = "Norway"
x =
getattr(Person, 'page', 'my message')
ဆက်စပ်စာမျက်နှာများ
attribute တစ်ခုကို ဖယ်ရှားရန် delattr () လုပ်ဆောင်ချက်
attribute တစ်ခုရှိ၊ မရှိ စစ်ဆေးရန် hasattr() လုပ်ဆောင်ချက်
attribute တစ်ခု၏ တန်ဖိုးကိုသတ်မှတ်ရန် setattr() လုပ်ဆောင်ချက်