An warware: ƙirƙiri fayil kuma shigo da shi azaman ɗakin karatu a cikin wani fayil ɗin

A cikin duniyar haɓaka software ta yau, yana da mahimmanci a kiyaye tsari da tsaftataccen ayyukan ƙididdigewa. Ɗayan irin wannan aikin shine ƙirƙirar fayiloli daban don takamaiman ayyuka da shigo da su azaman ɗakin karatu a cikin wasu fayiloli. Wannan ba kawai yana haɓaka iya karanta lambar ba har ma yana taimakawa wajen sake amfani da lambar. Wannan labarin zai jagorance ku kan yadda ake ƙirƙirar fayil da shigo da shi azaman ɗakin karatu a cikin wani fayil ta amfani da Python, sannan bayanin mataki-mataki na lambar. Bugu da ƙari, za mu bincika wasu ɗakunan karatu da ayyuka masu alaƙa waɗanda za su iya zama masu amfani ga masu haɓakawa.

Da farko, bari mu fahimci matsalar da ke hannun. A ce kuna da fayil ɗin Python mai ɗauke da ayyuka daban-daban, kuma kuna son amfani da waɗannan ayyukan a cikin wani fayil ɗin. Maimakon kwafa da liƙa lambar, shigo da fayil ɗin azaman ɗakin karatu na iya ceton ku duka lokaci da ƙoƙari, haɓaka haɓakar gabaɗaya.

Bi waɗannan matakan don ƙirƙirar fayil kuma shigo da shi azaman ɗakin karatu a cikin wani fayil ta amfani da Python:

1. Ƙirƙiri sabon fayil na Python tare da ayyukan da ake so.
2. Ajiye fayil ɗin tare da suna mai dacewa, misali, "my_library.py".
3. Yanzu, a cikin wani fayil ɗin Python, zaku iya shigo da wannan ɗakin karatu ta amfani da kalmar "import".

Ga bayanin mataki-mataki na lambar:

Da farko, ƙirƙiri sabon fayil ɗin Python mai suna “my_library.py” kuma ya haɗa da ayyuka masu zuwa:

def addition(a, b):
    return a + b

def multiplication(a, b):
    return a * b

Waɗannan ayyuka guda biyu suna yin ƙarawa da ayyukan haɓakawa, bi da bi.

Yanzu, bari mu ƙirƙiri wani fayil ɗin Python mai suna “main.py” inda za mu shigo da “my_library.py” ɗin mu:

import my_library

result1 = my_library.addition(3, 5)
result2 = my_library.multiplication(3, 5)

print("Addition: ", result1)
print("Multiplication: ", result2)

A cikin "main.py", mun fara shigo da fayil ɗin "my_library". Bayan haka, muna kiran ayyukan "ƙari" da "yawaita" daga "my_library.py" ta amfani da alamar digo. A ƙarshe, muna buga sakamakon ayyukan daban-daban.

Bayan aiwatar da "main.py", za ku ga fitarwa kamar:

““
Bugu: 8
Yawan yawa: 15
““

Python Shigo da Dakunan karatu

Python yana ba da ɗimbin ɗakunan karatu, wanda kuma aka sani da modules, waɗanda ke ba da damar aiwatar da ayyuka daban-daban cikin sauƙi. Kuna iya ƙirƙirar naku tsarin ko shigo da ginanniyar ɗakunan karatu waɗanda suka zo tare da Python.

Shigo da dakunan karatu tsari ne mai sauƙi: kawai kuna buƙatar amfani da kalmar "shigo da" da sunan ɗakin karatu ya biyo baya. Hakanan zaka iya amfani da kalmar "daga" don shigo da takamaiman ayyuka daga ɗakin karatu:

from my_library import addition

Anan, kuna shigo da aikin “ƙara” kawai daga “my_library.py”, kuma kuna iya amfani da shi kai tsaye ba tare da alamar digo ba.

Ayyuka da Fakiti

A aiki toshe ne na lambar da za a sake amfani da ita da ake amfani da ita don yin takamaiman aiki. Ayyuka suna taimakawa haɓaka iya karanta lambar da sake amfani da su. Kuna iya ayyana ayyukan ku, kamar yadda aka nuna a misalin da ke sama, ko amfani da ginanniyar ayyukan Python.

A kunshin tarin kayan aikin Python da dakunan karatu ne da aka tsara a cikin tsarin shugabanci. Yana sauƙaƙa tsarin sarrafawa da rarraba ɗakunan karatu da yawa da abin dogaronsu. Python yana da fakiti masu yawa don ayyuka daban-daban, kamar NumPy don lissafin lambobi, pandas don sarrafa bayanai, da TensorFlow don koyon injin.

A ƙarshe, ƙirƙirar fayiloli daban-daban don takamaiman ayyuka da shigo da su azaman ɗakunan karatu a cikin wasu fayilolin suna inganta tsarin lambar, karantawa, da kiyayewa a cikin ayyukan Python. Fahimtar bayanan shigo da kaya, ayyuka, da fakiti za su samar wa masu haɓaka kayan aiki masu mahimmanci don ingantattun ayyukan coding.

Shafi posts:

Leave a Comment