Ana sayfa Genel Python – Iteratorlar

Python – Iteratorlar

1470
86
PAYLAŞ

Iteratorların Oluşturulması ve Kullanılması

Bu konuda iteratorları oluşturmayı, kullanmayı ve kendi objelerimizi iterable (üzerinde gezinilebilecek) olarak nasıl yazarız öğrenmeye çalışacağız. İlk olarak iteratorlar nedir anlamaya çalışalım

Iteratorlar nedir?

Iteratorlar aslında Pythonda çoğu yerde biz görmesek de kullanılır. Iteratorlar özellikle for döngülerinde , list comprehensionlarında, ve bir sonraki derste göreceğimiz generatorlarda karşımıza çıkar.

Iteratorlar en genel anlamıyla üzerinde gezinilebilecek bir objedir ve bu obje her seferinde bir tane eleman döner.

Pythonda kendisinden iterator oluşturabileceğimiz her obje iterable bir objedir.. Örneğin, demetlerden,listelerden ve stringlerden oluşturduğumuz bütün objeler iterable bir objedir.

Bir objenin iterable olması için hazır metodlar olan __iter()__ ve __next()__ metodlarını mutlaka tanımlaması gerekir.
Iterator oluşturma

Bir iterator objesini , iterable bir objeden (liste,demet,string vs) oluşturmak için Pythonda iter() fonksiyonunu kullanıyoruz ve bu objenin bir sonraki elemanını almak için next() fonksiyonu kullanıyoruz.

İşte iterable bir objeden bir iterator’ı bu şekilde oluşturup, next() fonksiyonuyla objenin sıradaki elemanını alabiliyoruz. Ancak eleman kalmayınca StopIterationhatasını alıyoruz. İşte iteratorların kullanımı bu şekildedir. Aslında biz farketmesek de Pythondaki for döngüleri aslında bir objenin üzerinde gezinirken iteratorları kullanır.

Aslında for döngülerinin iç yapısı şu şekildedir;

 

Kendi Iterable Objelerimizi Oluşturmak

Peki biz kendi oluşturduğumuz veritiplerini nasıl iterable yapacağız ? Bunun için oluşturacağımız sınıfların mutlaka __iter()__ ve __next()__ metodlarını tanımlaması gereklidir. Şimdi bir tane kumanda sınıfı oluşturalım ve bu sınıfı iterable yapalım.

Güzel ! Objemizi iterable yapmayı başardık. Objemiz iterable olduğuna göre artık for döngüsüyle üzerinde gezinebiliriz.

İşte Pythonda iteratorların kullanımı bu şekilde ! Artık iterator kullanmayı ve kendi objelerimizi iterable yapmayı biliyoruz.

 

PAYLAŞ
Önceki makalePython – Decorator Fonksiyonlar
Sonraki makalePython – Generatorlar
Odtü Bilgisayar Mühendisliğinden 2017 senesinde mezun oldum ve şu anda aktif olarak eğitmenlik yapıyorum. Ayrıca şu anda Odtü Bilgisayar Mühendisliği Yüksek Lisansta akademik hayatımı sürdürüyorum.

86 YORUMLAR

  1. Please let me know if you’re looking for a article author for your blog. You have some really good articles and I believe I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some articles for your blog in exchange for a link back to mine. Please shoot me an email if interested. Regards!

  2. Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

  3. Admiring the time and effort you put into your site and detailed information you offer.
    It’s good to come across a blog every once in a while that isn’t the
    same out of date rehashed information. Excellent read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.

  4. I have been exploring for a little bit for any high quality articles or blog posts in this sort of house . Exploring in Yahoo I finally stumbled upon this web site. Studying this info So i am happy to convey that I have an incredibly good uncanny feeling I discovered exactly what I needed. I so much no doubt will make certain to don?¦t fail to remember this site and provides it a look on a relentless basis.

  5. Hi! This is kind of off topic but I need some advice from an established blog.
    Is it difficult to set up your own blog? I’m not very techincal but I can figure
    things out pretty quick. I’m thinking about making my own but
    I’m not sure where to begin. Do you have any tips or suggestions?
    Appreciate it

  6. Hi there, just changed into alert to your blog through Google, and found that it is truly informative. I am gonna be careful for brussels. I’ll appreciate should you proceed this in future. Numerous people can be benefited from your writing. Cheers!

  7. Great goods from you, man. I’ve understand your stuff previous to
    and you’re just too wonderful. I really like what you’ve acquired here,
    really like what you are saying and the way in which you
    say it. You make it enjoyable and you still care for to keep it wise.

    I can not wait to read much more from you. This is really a wonderful site.

  8. I just like the helpful information you supply in your articles. I’ll bookmark your weblog and test once more right here frequently. I am moderately certain I will be told many new stuff right here! Best of luck for the next!

  9. This blog is definitely rather handy since I’m at the moment creating an internet floral website – although I am only starting out therefore it’s really fairly small, nothing like this site. Can link to a few of the posts here as they are quite. Thanks much. Zoey Olsen

  10. Great V I should definitely pronounce, impressed with your site. I had no trouble navigating through all the tabs and related info ended up being truly simple to do to access. I recently found what I hoped for before you know it at all. Quite unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your customer to communicate. Excellent task..

  11. After research a number of of the weblog posts in your web site now, and I really like your method of blogging. I bookmarked it to my bookmark web site record and can be checking back soon. Pls take a look at my site as nicely and let me know what you think.

  12. I and my friends were actually looking at the good pointers on your website and so suddenly got an awful suspicion I had not thanked you for those techniques. Those people came so thrilled to read through them and have now surely been using those things. Thanks for genuinely considerably thoughtful and also for finding such useful subject matter most people are really desirous to learn about. Our honest regret for not expressing appreciation to you sooner.

  13. I’m impressed, I have to say. Really hardly ever do I encounter a weblog that’s each educative and entertaining, and let me tell you, you’ve gotten hit the nail on the head. Your concept is outstanding; the issue is something that not enough persons are talking intelligently about. I’m very pleased that I stumbled across this in my seek for one thing referring to this.

  14. Its fantastic as your other content : D, appreciate it for putting up. “Always be nice to people on the way up because you’ll meet the same people on the way down.” by Wilson Mizner.

  15. Great paintings! That is the type of information that are supposed to be shared around the web. Shame on the seek engines for now not positioning this put up higher! Come on over and consult with my web site . Thank you =)

  16. I’m curious to find out what blog system you happen to be working with? I’m experiencing some minor security issues with my latest site and I’d like to find something more safeguarded. Do you have any suggestions?

  17. You made some first rate points there. I seemed on the web for the difficulty and located most individuals will go together with together with your website.

  18. I like what you guys are up too. Such intelligent work and reporting! Keep up the excellent works guys I?¦ve incorporated you guys to my blogroll. I think it will improve the value of my web site 🙂

  19. I genuinely enjoy looking through on this site, it holds superb blog posts. “The living is a species of the dead and not a very attractive one.” by Friedrich Wilhelm Nietzsche.

  20. It’s truly a nice and helpful piece of info. I’m happy that you simply shared this helpful information with us. Please keep us up to date like this. Thank you for sharing.

  21. What’s Going down i am new to this, I stumbled upon this I have found It absolutely helpful and it has aided me out loads. I am hoping to contribute & aid other users like its helped me. Great job.

  22. Please let me know if you’re looking for a author for your blog.

    You have some really good articles and I believe I would be a good asset.

    If you ever want to take some of the load off, I’d absolutely love to write some material for your blog in exchange for a link back to mine.
    Please send me an email if interested. Thank you!

  23. I will immediately seize your rss as I can’t find your email subscription link or e-newsletter service. Do you have any? Please let me recognize so that I could subscribe. Thanks.

  24. Great post. I was checking constantly this blog and I am impressed! Very helpful information particularly the last part 🙂 I care for such info much. I was seeking this certain information for a very long time. Thank you and best of luck.

  25. I intended to draft you a bit of note so as to say thanks the moment again for the pretty advice you have provided here. It was simply generous with people like you to make extensively exactly what many of us might have offered for an e-book to get some profit for themselves, mostly seeing that you could possibly have tried it if you wanted. The things also served to become fantastic way to be aware that many people have the identical fervor similar to mine to find out great deal more when it comes to this problem. Certainly there are many more fun periods in the future for people who go through your site.

  26. Fantastic post however I was wondering if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Appreciate it!

  27. I like what you guys are up also. Such clever work and reporting! Keep up the superb works guys I?¦ve incorporated you guys to my blogroll. I think it’ll improve the value of my site 🙂

  28. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  29. Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

  30. I’ve been absent for a while, but now I remember why I used to love this web site. Thanks , I¦ll try and check back more frequently. How frequently you update your site?

  31. I have been surfing on-line greater than 3 hours as of late, but I never discovered any interesting article like yours.
    It is beautiful worth sufficient for me. In my view, if all webmasters and bloggers made excellent content as you did, the web will probably be much
    more helpful than ever before.

  32. FineScan 會在肌膚上製造數以千計的細小深入傷口,即所謂的顯微加熱區(microthermal zone),但要確保每次治療時皆有部份組織不受能量影響,於是,每一個顯微加熱區的作用雖然強烈而明顯,但周圍都包覆著正常且結構完整的皮膚組織,使傷口能在短時間內癒合,並替換之前有缺陷的受損組織。Finescan不僅可讓表皮新生,更可促進深層膠原再生,從內而外徹底喚醒細胞,瞬時找回年輕時的肌膚狀態。憑藉最新的雙軸技術,FINESCAN 6可治療 – 面部 – 頸部 – 暗瘡凹凸洞 – 增生性疤痕

  33. Thanks for the sensible critique. Me & my neighbor were just preparing to do a little research about this. We got a grab a book from our local library but I think I learned more clear from this post. I’m very glad to see such wonderful info being shared freely out there.

  34. Great article and straight to the point. I am not sure if this is really the best place to ask but do you guys have any ideea where to employ some professional writers? Thank you 🙂

  35. I’m really impressed together with your writing talents as neatly as with the format in your weblog. Is that this a paid subject matter or did you modify it yourself? Either way stay up the excellent high quality writing, it’s rare to peer a nice weblog like this one today..

  36. you’re in reality a good webmaster. The web site loading velocity is incredible. It sort of feels that you are doing any unique trick. Moreover, The contents are masterpiece. you have performed a excellent activity on this matter!

  37. I do like the way you have framed this particular concern and it really does offer us some fodder for thought. Nevertheless, coming from everything that I have personally seen, I simply just hope when the feed-back stack on that people today continue to be on issue and not get started upon a tirade involving some other news of the day. All the same, thank you for this exceptional piece and whilst I can not agree with this in totality, I respect your viewpoint.

  38. This blog is definitely rather handy since I’m at the moment creating an internet floral website – although I am only starting out therefore it’s really fairly small, nothing like this site. Can link to a few of the posts here as they are quite. Thanks much. Zoey Olsen

  39. I am now not certain the place you are getting your information, but great topic. I needs to spend a while finding out more or working out more. Thank you for great information I used to be searching for this information for my mission.

  40. Today, I went to the beach with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!

BİR CEVAP BIRAK

Lütfen yorumunuzu giriniz
Buraya isminizi giriniz