About the Tutorial


getattr(obj, name[, default])



Download 2,85 Mb.
Pdf ko'rish
bet221/234
Sana28.05.2022
Hajmi2,85 Mb.
#614232
1   ...   217   218   219   220   221   222   223   224   ...   234
Bog'liq
python3 tutorial 060821094426

getattr(obj, name[, default])
: to access the attribute of object. 

The
hasattr(obj,name)
: to check if an attribute exists or not. 

The
setattr(obj,name,value)
: to set an attribute. If attribute does not exist, then 
it would be created. 

The
delattr(obj, name)
: to delete an attribute. 
hasattr(emp1, 'salary') # Returns true if 'salary' attribute exists 
getattr(emp1, 'salary') # Returns value of 'salary' attribute 
setattr(emp1, 'salary', 7000) # Set attribute 'age' at 8 
delattr(emp1, 'salary') # Delete attribute 'age' 
Built-In Class Attributes 
Every Python class keeps the following built-in attributes and they can be accessed using 
dot operator like any other attribute − 

__dict__:
Dictionary containing the class's namespace. 

__doc__:
Class documentation string or none, if undefined. 

__name__:
Class name. 

__module__:
Module name in which the class is defined. This attribute is 
"__main__" in interactive mode. 


Python 3 
338 

__bases__:
A possibly empty tuple containing the base classes, in the order of 
their occurrence in the base class list. 
For the above class let us try to access all these attributes- 
#!/usr/bin/python3 
class Employee: 
'Common base class for all employees' 
empCount = 0 
def __init__(self, name, salary): 
self.name = name 
self.salary = salary 
Employee.empCount += 1 
def displayCount(self): 
print ("Total Employee %d" % Employee.empCount) 
def displayEmployee(self): 
print ("Name : ", self.name, ", Salary: ", self.salary) 
emp1 = Employee("Zara", 2000) 
emp2 = Employee("Manni", 5000) 
print ("Employee.__doc__:", Employee.__doc__) 
print ("Employee.__name__:", Employee.__name__) 
print ("Employee.__module__:", Employee.__module__) 
print ("Employee.__bases__:", Employee.__bases__) 
print ("Employee.__dict__:", Employee.__dict__ )
When the above code is executed, it produces the following result- 
Employee.__doc__: Common base class for all employees 
Employee.__name__: Employee 
Employee.__module__: __main__ 
Employee.__bases__: (,) 
Employee.__dict__: {'displayCount': , '__module__': '__main__', '__doc__': 
'Common base class for all employees', 'empCount': 2, '__init__': , 
'displayEmployee': , '__weakref__': , '__dict__': } 


Python 3 
339 

Download 2,85 Mb.

Do'stlaringiz bilan baham:
1   ...   217   218   219   220   221   222   223   224   ...   234




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish