Ana sayfa Genel Python – Iteratorlar

Python – Iteratorlar

7225
890
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.

890 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!

  41. I was just searching for this info for some time. After six hours of continuous Googleing, finally I got it in your web site. I wonder what is the lack of Google strategy that don’t rank this type of informative sites in top of the list. Usually the top web sites are full of garbage.

  42. Hey There. I found your blog using msn. This is a really well written article. I’ll be sure to bookmark it and return to read more of your useful information. Thanks for the post. I will definitely comeback.

  43. Nice post. I used to be checking constantly this blog and I’m inspired! Very useful info specifically the ultimate section 🙂 I deal with such information much. I was seeking this particular info for a long time. Thank you and best of luck.

  44. Hi there, You’ve done an excellent job. I’ll certainly digg it and personally recommend to my friends. I am sure they will be benefited from this web site.

  45. Usually I do not read post on blogs, but I would like to say that this write-up very forced me to try and do so! Your writing style has been amazed me. Thanks, very nice article.

  46. I have been browsing online more than 3 hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. Personally, if all site owners and bloggers made good content as you did, the internet will be a lot more useful than ever before.

  47. Thank you for the sensible critique. Me & my neighbor were just preparing to do a little research on this. We got a grab a book from our area library but I think I learned more from this post. I am very glad to see such wonderful information being shared freely out there.

  48. Have you ever considered creating an ebook or guest authoring on other websites? I have a blog centered on the same topics you discuss and would love to have you share some stories/information. I know my subscribers would value your work. If you’re even remotely interested, feel free to send me an e-mail.

  49. This is the suitable weblog for anybody who desires to search out out about this topic. You understand a lot its nearly arduous to argue with you (not that I truly would need…HaHa). You undoubtedly put a brand new spin on a subject thats been written about for years. Nice stuff, simply great!

  50. Hi, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me mad so any assistance is very much appreciated.

  51. 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.

  52. Excellent website you have here but I was wanting to know if you knew of any forums that cover the same topics discussed here? I’d really love to be a part of online community where I can get opinions from other experienced people that share the same interest. If you have any suggestions, please let me know. Thanks!

  53. I simply could not go away your site before suggesting that I actually loved the usual information an individual provide in your visitors? Is gonna be again regularly in order to check up on new posts.

  54. An impressive share, I just given this onto a colleague who was doing a little analysis on this. And he in fact bought me breakfast because I found it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I feel strongly about it and love reading more on this topic. If possible, as you become expertise, would you mind updating your blog with more details? It is highly helpful for me. Big thumb up for this blog post!

  55. I loved as much as you will receive carried out proper here. The comic strip is attractive, your authored material stylish. nonetheless, you command get got an impatience over that you would like be turning in the following. in poor health no doubt come further beforehand once more since precisely the similar just about very incessantly inside case you defend this hike.

  56. The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

  57. I have been surfing online more than 3 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. Personally, if all website owners and bloggers made good content as you did, the internet will be much more useful than ever before.

  58. Whats up this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding know-how so I wanted to get advice from someone with experience. Any help would be greatly appreciated!

  59. Great tremendous issues here. I am very glad to see your post. Thank you so much and i’m looking ahead to contact you. Will you please drop me a mail?

  60. I know this if off topic but I’m looking into starting my own blog and was wondering what all is needed to get set up? I’m assuming having a blog like yours would cost a pretty penny? I’m not very internet savvy so I’m not 100 certain. Any recommendations or advice would be greatly appreciated. Thanks

  61. Wonderful blog! I found it while surfing around on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Many thanks

  62. Today, I went to the beachfront with my kids. 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 put 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 completely off topic but I had to tell someone!

  63. I am curious to find out what blog system you are utilizing? I’m having some minor security problems with my latest site and I would like to find something more safeguarded. Do you have any suggestions?

  64. Nice post. I be taught one thing more difficult on totally different blogs everyday. It is going to always be stimulating to learn content from different writers and follow a bit one thing from their store. I’d favor to make use of some with the content material on my blog whether you don’t mind. Natually I’ll provide you with a hyperlink on your net blog. Thanks for sharing.

  65. I’d need to check with you here. Which isn’t something I usually do! I take pleasure in studying a submit that can make folks think. Additionally, thanks for allowing me to comment!

  66. 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

  67. I know this if off topic but I’m looking into starting my own blog and was wondering what all is required to get setup? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web smart so I’m not 100 positive. Any recommendations or advice would be greatly appreciated. Appreciate it

  68. Hello! Do you know if they make any plugins to safeguard against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?

  69. My partner and I stumbled over here different page and thought I may as well check things out. I like what I see so now i am following you. Look forward to finding out about your web page yet again.

  70. I savor, result in I discovered just what I used to be looking for. You have ended my 4 day lengthy hunt! God Bless you man. Have a nice day. Bye

  71. A powerful share, I just given this onto a colleague who was doing just a little evaluation on this. And he in truth bought me breakfast because I found it for him.. smile. So let me reword that: Thnx for the deal with! But yeah Thnkx for spending the time to discuss this, I really feel strongly about it and love reading extra on this topic. If attainable, as you become expertise, would you thoughts updating your blog with more details? It is highly helpful for me. Big thumb up for this weblog submit!

  72. Thank you for sharing superb informations. Your site is so cool. I am impressed by the details that you have on this blog. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for extra articles. You, my pal, ROCK! I found simply the info I already searched everywhere and just could not come across. What an ideal web-site.

  73. Thank you a lot for sharing this with all folks you really recognise what you are speaking about! Bookmarked. Kindly also talk over with my web site =). We may have a link trade agreement among us!

  74. You made some decent points there. I appeared on the internet for the issue and located most individuals will go along with together with your website.

  75. Hi there! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thank you for sharing!

  76. What¦s Taking place i’m new to this, I stumbled upon this I have discovered It positively useful and it has aided me out loads. I’m hoping to give a contribution & help other customers like its helped me. Great job.

  77. Hey very cool website!! Man .. Excellent .. Wonderful .. I’ll bookmark your site and take the feeds also…I am glad to seek out a lot of helpful info here within the post, we’d like develop more strategies in this regard, thank you for sharing.

  78. Hi, I think your blog might be having browser compatibility issues. When I look at your blog in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, excellent blog!

  79. I have not checked in here for a while as I thought it was getting boring, but the last few posts are great quality so I guess I¦ll add you back to my everyday bloglist. You deserve it my friend 🙂

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

  81. Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can’t wait to read lots of your posts. see this

  82. Thanks for all of your efforts on this website. My mother loves working on investigation and it is obvious why. We notice all about the dynamic means you deliver efficient tactics by means of this web blog and boost participation from the others on this topic then our princess is always becoming educated so much. Have fun with the remaining portion of the year. You are performing a fabulous job.

  83. I am really inspired with your writing skills and also with the structure to your blog. Is that this a paid topic or did you customize it your self? Either way stay up the nice high quality writing, it’s rare to look a great blog like this one these days..

  84. I simply couldn’t leave your site prior to suggesting that I extremely enjoyed the usual info an individual provide to your guests? Is going to be again often to check out new posts.

  85. Thanks for the sensible critique. Me & my neighbor were just preparing to do some 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 great info being shared freely out there.

  86. After examine a number of of the weblog posts on your web site now, and I truly like your approach of blogging. I bookmarked it to my bookmark website record and will likely be checking back soon. Pls take a look at my web site as nicely and let me know what you think.

  87. I have been browsing online greater than three hours today, yet I never discovered any fascinating article like yours. It is lovely value enough for me. Personally, if all webmasters and bloggers made good content material as you probably did, the net will be a lot more helpful than ever before.

  88. Hi there! I know this is kind of off topic but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one? Thanks a lot!

  89. What i do not understood is in truth how you are now not actually much more well-preferred than you may be right now. You’re very intelligent. You realize therefore considerably with regards to this topic, produced me in my opinion consider it from a lot of various angles. Its like men and women don’t seem to be involved unless it?¦s one thing to accomplish with Lady gaga! Your individual stuffs outstanding. Always care for it up!

  90. Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.

  91. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what youre talking about, why waste your intelligence on just posting videos to your weblog when you could be giving us something enlightening to read?

  92. Sweet blog! I found it while surfing around on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Many thanks

  93. I together with my friends were actually going through the good hints found on the blog and then instantly got an awful feeling I never thanked the blog owner for those tips. All the women ended up certainly very interested to learn all of them and have in effect in truth been loving these things. Thank you for simply being so kind and then for pick out variety of very good areas millions of individuals are really desirous to be aware of. My very own honest apologies for not expressing appreciation to earlier.

  94. Good – I should certainly pronounce, impressed with your website. I had no trouble navigating through all the tabs as well as 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 something, website theme . a tones way for your customer to communicate. Excellent task.

  95. Admiring the dedication you put into your website and detailed information you present. It’s awesome to come across a blog every once in a while that isn’t the same out of date rehashed material. Wonderful read! I’ve saved your site and I’m including your RSS feeds to my Google account.

  96. I like what you guys are up too. Such intelligent work and reporting! Keep up the superb works guys I’ve incorporated you guys to my blogroll. I think it will improve the value of my web site :).

  97. I’ve been absent for some time, but now I remember why I used to love this web site. Thank you, I’ll try and check back more often. How frequently you update your site?

  98. Attractive section of content. I just stumbled upon your site and in accession capital to claim that I acquire actually enjoyed account your weblog posts. Anyway I will be subscribing on your augment or even I success you access constantly quickly.

  99. This is the right blog for anyone who wants to find out about this topic. You realize so much its almost hard to argue with you (not that I actually would want…HaHa). You definitely put a new spin on a topic thats been written about for years. Great stuff, just great!