Ana sayfa Genel Python – Nesne Yönelimli Programlama – Sınıflar

Python – Nesne Yönelimli Programlama – Sınıflar


Nesne Yönelimli Programlama – Sınıflar

Bu konuyla beraber Nesne Tabanlı Programlamaya giriş yapıyoruz ve bu konuda biraz Nesne Tabanlı programlama hakkında konuşacağız. Nesne Yönelimli Programlama veya ingilizce ismiyle Object Oriented Programming en basit anlamıyla gerçek hayatı programlamaya uyarlamak olarak düşünülebilir. Örneğin bir tane öğrenci otomasyon sistemi yazmak istiyoruz. Bunun için öğretmenleri , öğrencileri ve kursları aslında birer nesne olarak oluşturmamız gerekiyor. Böyle bir sistemi programlamayla gerçekleştirmek için aslında her bir nesnenin yapısını tanımlayıp, daha sonra bu yapılardan nesneler üretmemiz gerekiyor. İşte Nesne Yönelimli Programlama en basit anlamıyla bu şekildedir. Şimdi isterseniz obje veya nesne nedir anlamaya çalışalım.

Obje nedir ?

Etrafımıza baktığımızda aslında her bir eşyanın bir obje olduğunu görüyoruz. Örneğin bir tane televizyon kumandasını düşünelim. Bu kumandanın kendi içinde değişik özellikleri (attribute) ve fonksiyonları(metod) bulunuyor. Örneğin, kumandanın markası, tuşları aslında bu kumandanın özellikleridir(attribute). Kumandanın kırmızı tuşuna bastığımızda televizyonun kapanması ve sesi kapatma tuşuna bastığımızda televizyonun sesinin kapanması bu kumandanın metodlarıdır. Bunun gibi Pythondaki aslında her şey bir objedir. Örneğin, listelere bakacak olursak bu liste objelerinin aslında birçok metodu ve özelliği bulunur.


Kendi veri tiplerimizi oluşturmak ve bu veri tiplerinden objeler üretmemiz için öncelikle objeleri üreteceğimiz yapıyı tanımlamamız gerekiyor. Bunu tasarladığımız yapıya da sınıf veya ingilizce ismiyle class diyoruz. Şimdi class yapılarını öğrenerek konumuza başlayalım.

Class Anahtar Kelimesi

Sınıflar veya Classlar objelerimizi oluştururken objelerin özelliklerini ve metodlarını tanımladığımız bir yapıdır ve biz herbir objeyi bu yapıya göre üretiriz. İsterseniz bir Araba classı tanımlayarak yapımızı kurmaya başlayalım.

Sınıfımızı Pythonda tanımladık. Peki bu sınıftan obje nasıl oluşturacağız ? Bunu da şu şekilde yapabiliyoruz.

araba1 objesi artık sınıfta tanımladığımız bütün özelliklere (attributes) sahip olmuş oldu. İşte sınıf ve obje üretmek bu şekilde olmaktadır. Peki bu araba objesinin özelliklerinin nasıl görebiliriz ?

Şimdi de başka bir Araba objesi oluşturalım.

Burda gördüğümüz gibi oluşturduğumuz objelerin buradaki model,renk vs. gibi özelliklerinin değeri aynıdır. Çünkü aslında burada tanımladığımız özellikler birer sınıf özelliğidir. Yani biz bir obje oluşturduğumuzda bu özelliklerin değerleri varsayılan olarak gelir. Bu özelliklerin değerlerine , herhangi bir obje oluşturmadan da erişebiliriz. Bunu da şu şekilde yapabiliriz.

Bizim her bir objeyi başlangıçta farklı değerlerle oluşturmamız için her bir objeyi oluştururken objelerin değerlerini göndermemiz gerekiyor. Bunun için de özel bir metodu kullanmamız gerekmektedir.

Peki bu metod ne anlama geliyor ? İsterseniz ilk olarak dir() fonksiyonu yardımıyla araba1 objemizde neler var bakalım.

Burada objemizin tüm özelliklerini ve metodlarını görüyoruz. Ancak biz herhangi bir metod tanımlamamıştır. Buradaki metodlar Python tarafından bir obje oluşturulduğunda özel olarak tanımlanan metodlardır ve biz eğer özel olarak bunları tanımlamazsak Python kendisi bunları varsayılan tanımlıyor. Burada aynı zamanda init metodunu da görüyoruz. Eğer biz bu metodu kendimiz tanımlarsak objelerimizi farklı değerlerle başlatabiliriz.

Aslında init metodu Pythonda yapıcı(constructor ) fonksiyon olarak tanımlanmaktadır. Bu metod objelerimiz oluşturulurken otomatik olarak ilk çağrılan fonksiyondur. Bu metodu özel olarak tanımlayarak objelerimizi farklı değerlerle oluşturabiliriz.

Peki bu metodu nasıl tanımlayacağız ? Direk örnek üzerinden görmeye çalışalım.

Peki burada self ne anlama geliyor ? self anahtar kelimesi objeyi oluşturduğumuz zaman o objeyi gösteren bir referanstır ve metodlarımızda en başta bulunması gereken bir parametredir. Yani biz bir objenin bütün özelliklerini ve metodlarını bu referans üzerinden kullanabiliriz.

Objeler oluşturulurken, Python bu referansı metodlara otomatik olarak kendisi gönderir. Özel olarak self referansını göndermemize gerek yoktur.

init metodunu ve self’i iyi anlamak için objelerimize özellikler ekleyelim.

İstersek init metodunu varsayılan değerlerle de yazabiliriz.

İşte burada gördüğümüz gibi bir objeyi init metodunu kendimiz yazarak farklı değerlerle oluşturabiliyoruz. Bir sonraki derste objelere kendi metodlarımızı nasıl yazacağımızı öğrenmeye çalışacağız.


  1. I believe that is one of the such a lot important information for me. And i’m happy studying your article. However should remark on few general issues, The web site style is ideal, the articles is really excellent : D. Just right activity, cheers

  2. Thanks a bunch for sharing this with all of us you actually know what you are talking about! Bookmarked. Kindly also visit my website =). We could have a link exchange agreement between us!

  3. 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 throw away your intelligence on just posting videos to your site when you could be giving us something enlightening to read?

  4. Thank you, I’ve just been looking for info about this subject for a while and yours is the greatest I’ve discovered till now. But, what about the conclusion? Are you sure about the source?

  5. Thank you for the auspicious writeup. It actually was a amusement account it. Glance advanced to more brought agreeable from you! By the way, how could we keep in touch?

  6. Hi , I do believe this is an excellent blog. I stumbled upon it on Yahoo , i will come back once again. Money and freedom is the best way to change, may you be rich and help other people.

  7. Hi there! This post could not be written any better! Reading through this post reminds me of my good old room mate! He always kept chatting about this. I will forward this write-up to him. Fairly certain he will have a good read. Many thanks for sharing!

  8. I?¦m no longer sure the place you are getting your info, however great topic. I needs to spend a while learning much more or figuring out more. Thank you for magnificent info I used to be looking for this info for my mission.

  9. Nice post. I learn something totally new and challenging on blogs
    I stumbleupon everyday. It will always be helpful to read through
    articles from other writers and practice something from their websites.

  10. Wonderful blog! I found it while browsing on Yahoo News.
    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!

  11. Great blog right here! Also your web site a lot up fast! What host are you using? Can I get your associate hyperlink to your host? I wish my site loaded up as fast as yours lol

  12. Hey there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Firefox. I’m not sure if this is a format issue or something to do with web browser compatibility but I thought I’d post to let you know. The design and style look great though! Hope you get the problem solved soon. Kudos

  13. Its like you learn my mind! You seem to know a lot
    approximately this, like you wrote the guide
    in it or something. I think that you can do with some percent to force the message house a bit, however other than that, that is magnificent blog.
    A great read. I’ll certainly be back.

  14. I don’t even know how I stopped up here, however I believed this submit was great. I don’t realize who you might be but definitely you’re going to a well-known blogger in the event you are not already 😉 Cheers!

  15. Nice blog right here! Also your web site so much up
    very fast! What host are you the usage of? Can I am getting your associate hyperlink on your host?

    I desire my site loaded up as quickly as yours lol

  16. Howdy this is kinda of off topic but I was wondering if
    blogs use WYSIWYG editors or if you have to manually code with HTML.

    I’m starting a blog soon but have no coding skills so I wanted to
    get advice from someone with experience. Any help
    would be greatly appreciated!

  17. Thanks for some other informative site. Where else may I get that type of information written in such an ideal way? I have a mission that I’m just now running on, and I have been at the look out for such info.

  18. I’ve been browsing online more than three hours nowadays, but I never discovered any fascinating article like yours. It?¦s pretty price sufficient for me. Personally, if all webmasters and bloggers made just right content as you probably did, the internet will likely be a lot more helpful than ever before.

  19. Hi, I think your blog might be having browser compatibility issues. When I look at your website in Firefox, 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, terrific blog!

  20. This is the suitable blog for anybody who needs to search out out about this topic. You realize so much its virtually arduous to argue with you (not that I truly would need…HaHa). You positively put a new spin on a subject thats been written about for years. Nice stuff, simply nice!

  21. Hiya, I’m really glad I’ve found this info. Nowadays bloggers publish only about gossips and net and this is actually irritating. A good website with exciting content, this is what I need. Thank you for keeping this web site, I will be visiting it. Do you do newsletters? Cant find it.

  22. Thanks for sharing excellent informations. Your site is very cool. I’m impressed by the details that you have on this site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. You, my friend, ROCK! I found just the information I already searched everywhere and simply could not come across. What a great web-site.

  23. Hi there would you mind letting me know which hosting company you’re working with? I’ve loaded your blog in 3 different web browsers and I must say this blog loads a lot quicker then most. Can you recommend a good internet hosting provider at a honest price? Kudos, I appreciate it!

  24. SMAZ換能器傳送更穩定,更安全,更有效,和疼痛更少的HIFU能量。 SMAZ不需要應用較高的能量便可到達浅肌肉腱膜層。主要治療:-抬頭紋 -眉間紋 -鼻樑紋 -魚尾紋 -法令紋 -淚溝 -木偶紋 -垂直唇紋 -嘴角紋 -頸紋

  25. Great post. I was checking constantly this blog and I am impressed! Very useful information specifically the last part 🙂 I care for such info much. I was looking for this particular information for a long time. Thank you and good luck.

  26. The very core of your writing while sounding reasonable originally, did not work very well with me personally after some time. Someplace throughout the sentences you actually were able to make me a believer but only for a very short while. I still have got a problem with your leaps in assumptions and one might do nicely to help fill in those gaps. In the event that you can accomplish that, I could certainly be impressed.

  27. Hi! I know this is kinda off topic but I was wondering which blog platform are you using for this site? I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking at options for another platform. I would be awesome if you could point me in the direction of a good platform.

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

  29. Newport manager Michael Flynn has signed an improved new contract with the Sky Bet League Two club. Flynn, whose side take on Tottenham on Wednesday, has extended his deal until 2020. Newport manager Michael Flynn signs improved contract ahead of FA Cup replay against Tottenham at Wembley

  30. Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something.
    I think that you could do with some pics to drive
    the message home a little bit, but instead of that, this
    is excellent blog. An excellent read. I’ll definitely
    be back.

  31. Some really superb posts on this web site, thank you for contribution. “When he has ceased to hear the many, he may discern the One – the inner sound which kills the outer.” by H Hahn Blavatsky.

  32. What’s Happening i’m new to this, I stumbled upon this I have found It absolutely helpful and it has helped me out loads. I hope to contribute & aid other users like its aided me. Good job.

  33. When I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Thanks!

  34. An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers

  35. With havin so much content do you ever run into any problems of plagorism or copyright infringement? My site has a lot of unique content I’ve either created myself or outsourced but it looks like a lot of it is popping it up all over the internet without my authorization. Do you know any techniques to help stop content from being stolen? I’d truly appreciate it.

  36. Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across. It extremely helps make reading your blog significantly easier.

  37. I’m really impressed with your writing abilities and also with the layout to your weblog. Is that this a paid topic or did you customize it yourself? Either way keep up the excellent quality writing, it’s uncommon to see a nice blog like this one today..

  38. Fantastic beat ! I would like to apprentice even as you amend your web site, how could i subscribe for a weblog web site? The account aided me a acceptable deal. I had been tiny bit familiar of this your broadcast provided vibrant transparent idea

  39. Aw, this was a really nice post. In thought I wish to put in writing like this additionally – taking time and actual effort to make a very good article… however what can I say… I procrastinate alot and on no account seem to get one thing done.

  40. This is the proper weblog for anybody who wants to seek out out about this topic. You notice so much its almost laborious to argue with you (not that I really would need…HaHa). You positively put a brand new spin on a topic thats been written about for years. Great stuff, just great!

  41. Hi there just wanted to give you a quick heads up. The text in your content seem to be running off the screen in Ie. I’m not sure if this is a format issue or something to do with browser compatibility but I thought I’d post to let you know. The style and design look great though! Hope you get the issue fixed soon. Thanks

  42. you’re actually a just right webmaster. The site loading speed is amazing. It kind of feels that you’re doing any distinctive trick. Also, The contents are masterpiece. you have done a magnificent task on this matter!

  43. Hello, Neat post. There’s an issue along with your site in web explorer, could check this?K IE nonetheless is the marketplace leader and a huge section of other people will pass over your magnificent writing because of this problem.

  44. Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you can do with a few pics to drive the message home a bit, but instead of that, this is great blog. An excellent read. I will definitely be back.

  45. You actually make it seem so easy with your presentation but I find this topic to be really something that I think I would never understand. It seems too complicated and extremely broad for me. I’m looking forward for your next post, I’ll try to get the hang of it!

  46. In this grand scheme of things you receive a B- with regard to effort and hard work. Where exactly you confused everybody was on all the specifics. As it is said, the devil is in the details… And that could not be more correct right here. Having said that, permit me inform you exactly what did do the job. Your authoring is certainly incredibly powerful and that is probably the reason why I am taking the effort in order to opine. I do not really make it a regular habit of doing that. Secondly, although I can easily notice the leaps in logic you make, I am not really certain of exactly how you appear to connect your points which inturn help to make your final result. For now I shall yield to your position but wish in the future you actually link your dots better.

  47. Howdy! I know this is kinda off topic however I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog article or vice-versa? My site goes over a lot of the same topics as yours and I think we could greatly benefit from each other. If you happen to be interested feel free to shoot me an email. I look forward to hearing from you! Fantastic blog by the way!

  48. I’ve been absent for a while, but now I remember why I used to love this blog. Thank you, I’ll try and check back more often. How frequently you update your site?

  49. Greetings from Los angeles! I’m bored at work so I decided to check out your blog on my iphone during lunch break. I enjoy the information you provide here and can’t wait to take a look when I get home. I’m surprised at how quick your blog loaded on my mobile .. I’m not even using WIFI, just 3G .. Anyways, excellent blog!

  50. You really make it seem so easy with your presentation but I find this topic to be actually something which I think I would never understand. It seems too complicated and very broad for me. I’m looking forward for your next post, I will try to get the hang of it!

  51. Hi , I do believe this is an excellent blog. I stumbled upon it on Yahoo , i will come back once again. Money and freedom is the best way to change, may you be rich and help other people.

  52. Nice post. I was checking constantly this blog and I’m impressed! Extremely helpful info specially the last part 🙂 I care for such information much. I was seeking this particular information for a long time. Thank you and good luck.

  53. Howdy would you mind stating which blog platform you’re using? I’m going to start my own blog soon but I’m having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique. P.S My apologies for being off-topic but I had to ask!

  54. 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!

  55. We’re a group of volunteers and starting a new scheme in our community. Your site offered us with valuable information to work on. You’ve done an impressive job and our whole community will be grateful to you.

  56. I’ve been surfing on-line more than 3 hours nowadays, yet I never found any interesting article like yours. It’s lovely worth sufficient for me. In my view, if all website owners and bloggers made good content material as you probably did, the net will likely be much more helpful than ever before.

  57. Hello! I could have sworn I’ve been to your blog before but after looking at many of the articles I realized it’s new to me. Anyhow, I’m definitely pleased I stumbled upon it and I’ll be book-marking it and checking back regularly!

  58. It’s actually a cool and helpful piece of info. I’m glad that you shared this useful information with us. Please keep us informed like this. Thanks for sharing.

  59. After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.

  60. certainly like your web-site however you need to check the spelling on several of your posts. Many of them are rife with spelling problems and I in finding it very troublesome to tell the truth nevertheless I will certainly come back again.

  61. My wife and i were ecstatic that Emmanuel managed to carry out his researching through your ideas he grabbed using your web page. It is now and again perplexing just to possibly be releasing concepts which often some others may have been making money from. So we do know we have the website owner to thank for that. Most of the explanations you have made, the simple site navigation, the relationships you can make it possible to create – it’s mostly excellent, and it is aiding our son in addition to our family imagine that this concept is awesome, which is certainly very serious. Many thanks for all the pieces!

  62. Amazing! This blog looks just like my old one! It’s on a completely different subject but it has pretty much the same layout and design. Excellent choice of colors!

  63. Wow, awesome weblog structure! How lengthy have you been running a blog for? you made running a blog look easy. The overall glance of your web site is magnificent, as neatly as the content!

  64. What i do not understood is in truth how you’re not actually a lot more smartly-preferred than you might be now. You are so intelligent. You understand therefore considerably relating to this subject, made me in my opinion believe it from numerous various angles. Its like men and women are not fascinated except it?¦s something to do with Woman gaga! Your personal stuffs outstanding. Always take care of it up!

  65. I am not sure where you’re getting your info, but good topic. I needs to spend some time learning more or understanding more. Thanks for wonderful info I was looking for this info for my mission.

  66. Very efficiently written post. It will be valuable to everyone who usess it, as well as myself. Keep doing what you are doing – looking forward to more posts.

  67. Together with almost everything which seems to be building throughout this specific subject matter, all your perspectives are actually somewhat radical. Having said that, I beg your pardon, but I can not give credence to your entire plan, all be it radical none the less. It looks to everyone that your commentary are generally not completely validated and in reality you are generally yourself not even completely certain of the argument. In any case I did take pleasure in reading through it.

  68. Thanks for another informative website. Where else could I get that type of info written in such a perfect way? I have a project that I am just now working on, and I have been on the look out for such info.

  69. Thanks for your marvelous posting! I seriously enjoyed reading it,you could be a great author. I will be sure to bookmark your blog and will often come back later on. I want to encourage that you continue your great job, have a nice evening!Also visit my web site;

  70. A lot of thanks for your entire work on this blog. My aunt delights in doing investigations and it is obvious why. My spouse and i hear all concerning the powerful manner you render good guidance via the web site and in addition welcome participation from others on the area and our own child is without a doubt understanding so much. Take pleasure in the rest of the year. You’re conducting a powerful job.

  71. I don’t know whether it’s just me or if perhaps everyone
    else encountering problems with your website. It appears as though some of the
    text within your posts are running off the screen. Can someone else please provide
    feedback and let me know if this is happening to them too?
    This could be a problem with my web browser because I’ve had this happen previously.



Please enter your comment!
Please enter your name here