Ana sayfa Genel Python – Decorator Fonksiyonlar

Python – Decorator Fonksiyonlar

1628
86
PAYLAŞ

Decorator Fonksiyonlar

İç içe Fonksiyonlar ve Fonksiyon Parametreleri

Bu bölümle birlikte decorator fonksiyonların mantığını anlamaya çalışacağız. Decoratorları daha iyi anlamak için fonksiyonlara biraz daha derinlemesine bakmaya çalışalım.

*args ve **kwargs

Fonksiyonlara argüman göndermenin esnek bir şekilde yapıldığını biliyoruz. İsterseniz ilk olarak yıldızlı argümanları hatırlamaya çalışalım.

Şimdi de yeni öğreneceğimiz keyword argümanlara bakalım.

Dikkat ederseniz 2 yıldız koyarak keyword argümanlar ile (anahtar kelimeli argümanlar) fonksiyonumuzu tanımladık ve argümanlara isim vererek fonksiyonumuzu çağırdığımızda isimleri anahtar kelime , argüman değerlerini değer olarak alarak fonksiyonumuz sözlük oluşturdu. İşte keyword argümanlar bu şekilde kullanılabiliyor.

Şimdi de *args ve **kwargs argümanlarını beraber kullanalım.

İç içe fonksiyonlar

Pythonda fonksiyonlar da birer obje oldukları için hem bir tane değişkene atanabilirler, hem de başka bir fonksiyonun içinde tanımlanabilirler. Şimdi bunlara bakmaya başlayalım.

Şimdi de isterseniz iç içe fonksiyon tanımlamayı öğrenelim.

Fonksiyonları Dönmek ve Argüman Olarak Göndermek

Bu konuda fonksiyonları return ile başka bir fonksiyondan dönmeyi ve diğer fonksiyonlara parametre olarak göndermeyi öğreneceğiz.

Fonksiyonları return ile Dönmek

Bir fonksiyon aynı zamanda bir obje olduğu için, bu fonksiyonu başka bir fonksiyondan return ile döndürebiliriz.

İşte bu şekilde fonksiyonlarımızı da tıpkı diğer değişkenler gibi başka bir fonksiyon içinde tanımlayıp , çağırdığımız yere dönebiliriz.

Fonksiyonları Argüman Olarak Göndermek

Buradaki gibi her fonksiyonumuz aslında birer obje olduğu için diğer objeler gibi başka fonksiyonlara argüman olarak gönderilebilir. Bunları da öğrendiğimize göre artık decoratorlar için hazırız.

Decorator Fonksiyonların Oluşturulması ve Kullanılması

Bu konuda decorator konseptini anlamaya çalışacağız.

Decorator fonksiyonlar, Pythonda fonksiyonlarımıza dinamik olarak ekstra özellik eklediğimiz fonksiyonlardır ve decorator fonksiyonların kullanımı kod tekrarı yapmamızı engeller. Pythonda decorator fonksiyonlar Flask gibi frameworklerde oldukça fazla kullanılır.

Bu iki fonksiyonda fonksiyonların çalışma sürelerini ölçmek için time modülünü kullandık. Ancak dikkat ederseniz, hem bu fonksiyonlara ekstradan iş yaptırdık , hem de 2 fonksiyonda da aynı satırları yazdık. Yani, aslında kod tekrarına düştük. Ancak eğer bu fonksiyonlara ekstra özellik ekleyen decorator bir fonksiyonumuz olsaydı, burada ne kod tekrarına düşecektik ne de fonksiyonlara ekstradan satır ekleyecektik. İşte decoratorların tam olarak önemi budur. İsterseniz zaman_hesapla isimli decorator’ımızı ilk önce yazalım. Daha sonra decoratorları açıklamaya çalışalım.

Burada zaman_hesapla() isimli decorator bir fonksiyon yazdık. Örneğin kareleri_hesapla(range(100000)) fonksiyonu çağrılırken aslında senaryomuz şu şekilde çalışıyor;

İşte decorator fonksiyonlarımızın aslında işlevi tamamıyla bu şekilde. Burada gördüğümüz gibi decoratorlar sayesinde, hem kod tekrarından kurtulduk, hem de zaman hesaplama işlemimizi decorator fonksiyonumuzda gerçekleştirdik.

86 YORUMLAR

  1. I am really enjoying the theme/design of your website. Do you ever run into any web browser compatibility problems? A number of my blog audience have complained about my blog not operating correctly in Explorer but looks great in Safari. Do you have any recommendations to help fix this problem?

  2. you’re actually a just right webmaster. The site loading pace
    is amazing. It seems that you’re doing any distinctive trick.
    In addition, The contents are masterpiece. you have performed a magnificent activity on this matter!

  3. Hey there I am so glad I found your webpage, I really found
    you by accident, while I was researching on Yahoo for something else, Nonetheless I am here now and would
    just like to say kudos for a fantastic post and a
    all round enjoyable blog (I also love the theme/design), I don’t have time
    to read it all at the moment but I have bookmarked it and also added your RSS feeds, so when I have time I will be
    back to read a great deal more, Please do keep up the awesome job.

  4. Hmm is anyone else encountering problems with the pictures on this blog loading? I’m trying to determine if its a problem on my end or if it’s the blog. Any suggestions would be greatly appreciated.

  5. Hiya very cool site!! Guy .. Excellent .. Amazing .. I’ll bookmark your website and take the feeds also…I am glad to seek out so many useful information here in the post, we want work out more strategies on this regard, thanks for sharing.

  6. Great – I should definitely pronounce, impressed with your website. I had no trouble navigating through all tabs as well as related information ended up being truly simple to do to access. I recently found what I hoped for before you know it at all. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, website theme . a tones way for your customer to communicate. Excellent task.

  7. Excellent weblog here! Additionally your website loads up very fast! What host are you the use of? Can I am getting your associate hyperlink on your host? I want my website loaded up as quickly as yours lol

  8. I’m not sure why but this weblog is loading very slow for me. Is anyone else having this issue or is it a issue on my end? I’ll check back later on and see if the problem still exists.

  9. I love your blog.. very nice colors & theme. Did you create this website yourself? Plz reply back as I’m looking to create my own blog and would like to know wheere u got this from. thanks

  10. I have been absent for some time, but now I remember why I used to love this website. Thank you, I will try and check back more often. How frequently you update your site?

  11. Greetings from California! I’m bored to death at work so I decided to check out your blog on my iphone during lunch break. I love the knowledge you provide here and can’t wait to take a look when I get home. I’m shocked at how fast your blog loaded on my phone .. I’m not even using WIFI, just 3G .. Anyways, fantastic blog!

  12. I’ve been browsing online more than three hours nowadays, but I never found any fascinating article like yours. It’s lovely value sufficient for me. In my opinion, if all webmasters and bloggers made good content material as you did, the internet might be much more helpful than ever before.

  13. It?¦s actually a nice and helpful piece of info. I?¦m satisfied that you shared this helpful info with us. Please keep us up to date like this. Thank you for sharing.

  14. There are certainly quite a lot of details like that to take into consideration. That is a great level to deliver up. I offer the ideas above as normal inspiration however clearly there are questions just like the one you bring up the place crucial factor can be working in honest good faith. I don?t know if finest practices have emerged round issues like that, but I’m positive that your job is clearly identified as a fair game. Each girls and boys feel the impact of just a second’s pleasure, for the rest of their lives.

  15. Sling tv coupons and promo codes for november 2018
    Hi there, i read your blog occasionally and i own a similar one and i was just wondering
    if you get a lot of spam feedback? If so how do you prevent it,
    any plugin or anything you can advise? I get so much lately it’s driving me mad so any
    help is very much appreciated. Sling tv coupons and promo
    codes for november 2018

  16. I was curious if you ever thought of changing the layout of your website? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or 2 images. Maybe you could space it out better?

  17. I am often to blogging and i really appreciate your content. The article has really peaks my interest. I am going to bookmark your site and keep checking for new information.

  18. Very nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed surfing around your blog posts. In any case I’ll be subscribing to your rss feed and I hope you write again very soon!

  19. The other day, while I was at work, my sister stole my apple ipad and tested to see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views. I know this is totally off topic but I had to share it with someone!

  20. Heya! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no backup. Do you have any methods to prevent hackers?

  21. Keep up the superb piece of work, I read few articles on this website and I believe that your blog is really interesting and contains bands of great information.

  22. Can I simply just say what a comfort to find someone that truly knows what they are
    talking about on the web. You definitely realize how to bring an issue to light and make it important.
    A lot more people ought to read this and understand this side of the story.
    I was surprised that you aren’t more popular given that you definitely have the gift.

  23. 紅石榴保濕噴霧 重水保濕噴霧 清肌保濕噴霧 瑕疵遮瑕膏 修容霜 護眼修飾霜 眼部遮瑕修護筆 亮白蓋斑霜 香茅SPF30 香茅蚊怕水 古銅暗影粉2號 胭脂輪廓修飾 點綴立體修容 奇幻腮紅 亮麗持久胭脂膏 深層修護滋養潤唇霜 保濕護唇膏

  24. I do agree with all of the ideas you have presented in your post. They’re really convincing and will certainly work. Still, the posts are too short for newbies. Could you please extend them a little from next time? Thanks for the post.

  25. Hello very cool web site!! Guy .. Beautiful .. Superb .. I’ll bookmark your site and take the feeds additionally…I am glad to seek out numerous useful info here within the put up, we’d like develop more strategies in this regard, thank you for sharing.

  26. Oh my goodness! an amazing article dude. Thank you Nevertheless I’m experiencing concern with ur rss . Don’t know why Unable to subscribe to it. Is there anyone getting equivalent rss downside? Anybody who knows kindly respond. Thnkx

  27. Awsome info and straight to the point. I don’t know if this is really the best place to ask but do you folks have any thoughts on where to get some professional writers? Thx 🙂

  28. IAN LADYMAN: Almost 16 years on from Claudio Ranieri’s first game in English football at Old Trafford, he heads back with Leicester City on the verge of the Premier League title. Claudio Ranieri says it’s still too soon for Leicester City to party: ‘We’re close but it’s important now that we finish the story’

  29. Thanks for another informative web site. Where else could I get that kind of info written in such a perfect way? I have a project that I’m just now working on, and I’ve been on the look out for such information.

  30. Hi! Quick question that’s completely off topic. Do you know how to make your site mobile friendly? My website looks weird when viewing from my iphone 4. I’m trying to find a template or plugin that might be able to fix this problem. If you have any recommendations, please share. Many thanks!

  31. You actually make it appear so easy with your presentation however I find this topic to be really one thing which I feel I would never understand. It seems too complicated and very extensive for me. I’m looking ahead to your subsequent put up, I will try to get the dangle of it!

  32. I was just searching for this info for a while. After 6 hours of continuous Googleing, finally I got it in your site. I wonder what’s the lack of Google strategy that don’t rank this kind of informative web sites in top of the list. Normally the top web sites are full of garbage.

  33. I have been exploring for a little for any high-quality articles or blog posts on this sort of area . Exploring in Yahoo I finally stumbled upon this website. Reading this info So i am satisfied to exhibit that I have a very good uncanny feeling I discovered exactly what I needed. I most certainly will make sure to do not fail to remember this web site and give it a look on a continuing basis.

BİR CEVAP BIRAK

Lütfen yorumunuzu giriniz
Buraya isminizi giriniz