Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming


Accessing Class Attributes in Methods



Download 2,61 Mb.
bet113/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   109   110   111   112   113   114   115   116   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Accessing Class Attributes in Methods


Within the methods you create, you’ll often need access to attributes defined within the class. To do so, you need to use the self keyword in order to access the attribute. Remember that self is in reference to the instance accessing the class. When we create multiple instances, self is what allows the program to understand which sound attribute to return. This is true even for global attributes. Let’s see an example:

# using the self keyword to access attributes within class methods class Dog( ):
sound = "bark" def makeSound(self):
print(self.sound) # self required to access attributes defined in the class
sam = Dog( ) sam.makeSound( )

Go ahead and run the cell. We’ll get an output of “bark” again, except this time, it was because we accessed the sound attribute declared within the class. Anytime you need to reference an attribute using self, you must include self within the method parameters.

Method Scope


Like global attributes, you may have methods that are accessible through the class itself rather than an instance of the class. These may also be known as static methods. They are not accessible by instances of the class. Depending on the class your building, it may help to have a method that is only accessible through the class and not the instances. Let’s see an example:

1| # understanding which methods are accessible via the class itself and class instances
3| class Dog( ):
4| sound = "bark" 6| def makeSound(self):
7| print(self.sound) 9| def printInfo( ):
10| print("I am a dog.")
12| Dog.printInfo( ) # able to run printInfo method because it does not include self parameter
14| # Dog.makeSound( ) would produce error, self is in reference to instances only
16| sam = Dog( )
18| sam.makeSound( ) # able to access, self can reference the instance of sam 20| # sam.printInfo( ) will produce error, instances require the self parameter to access methods

Go ahead and run the cell. We’ve defined two methods within our Dog class this time. One method has self within the parameter, while the other does not. The method without the self parameter can be accessed through the class itself, which is why line 8 outputs “I am a dog.”. The 9th line is commented out because makeSound can only be accessed by instances of our Dog class, not the class itself. Lastly, we can see that the 12th line is also commented out because methods that are not defined with self as a parameter cannot be accessed by instances of the class. Otherwise, we would produce an error. This is the importance of the self keyword.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   109   110   111   112   113   114   115   116   ...   200




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