Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet54/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   50   51   52   53   54   55   56   57   ...   183
Bog'liq
python3-oop

[
 45 
]
Actually, no. This is the typical order of things in a Python program, but it's not the 
only possible layout. Classes 
can be defined anywhere. They are typically defined at 
the module level, but they can also be defined inside a function or method, like this:
def format_string(string, formatter=None):
'''Format a string using the formatter object, which
is expected to have a format() method that accepts
a string.'''
class DefaultFormatter:
'''Format a string in title case.'''
def format(self, string):
return str(string).title()
if not formatter:
formatter = DefaultFormatter()
return formatter.format(string)
hello_string = "hello world, how are you today?"
print(" input: " + hello_string)
print("output: " + format_string(hello_string))
The output will be as follows:
input: hello world, how are you today?
output: Hello World, How Are You Today?
The 
format_string
function accepts a string and optional formatter object, and then 
applies the formatter to that string. If no formatter is supplied, it creates a formatter 
of its own as a local class and instantiates it. Since it is created inside the scope of 
the function, this class cannot be accessed from anywhere outside of that function. 
Similarly, functions can be defined inside other functions as well; in general, any 
Python statement can be executed at any time.
These inner classes and functions are occasionally useful for one-off items that don't 
require or deserve their own scope at the module level, or only make sense inside a 
single method. However, it is not common to see Python code that frequently uses 
this technique.
www.it-ebooks.info


Objects in Python
[

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   50   51   52   53   54   55   56   57   ...   183




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