This commit is contained in:
Eray Aydın 2015-03-25 21:36:52 +02:00
parent 71d6883796
commit e34c67290a

View File

@ -479,59 +479,57 @@ filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7]
[x for x in [3, 4, 5, 6, 7] if x > 5] # => [6, 7] [x for x in [3, 4, 5, 6, 7] if x > 5] # => [6, 7]
#################################################### ####################################################
## 5. Classes ## 5. Sınıflar
#################################################### ####################################################
# We subclass from object to get a class. # Sınıf oluşturmak için objeden alt sınıf oluşturacağız.
class Human(object): class Insan(obje):
# A class attribute. It is shared by all instances of this class # Sınıf değeri. Sınıfın tüm nesneleri tarafından kullanılabilir
species = "H. sapiens" tur = "H. sapiens"
# Basic initializer, this is called when this class is instantiated. # Basit başlatıcı, Sınıf çağrıldığında tetiklenecektir.
# Note that the double leading and trailing underscores denote objects # Dikkat edin, iki adet alt çizgi(_) bulunmakta. Bunlar
# or attributes that are used by python but that live in user-controlled # python tarafından tanımlanan isimlerdir.
# namespaces. Methods(or objects or attributes) like: __init__, __str__, # Kendinize ait bir fonksiyon oluştururken __fonksiyon__ kullanmayınız!
# __repr__ etc. are called magic methods (or sometimes called dunder methods) def __init__(self, isim):
# You should not invent such names on your own. # Parametreyi sınıfın değerine atayalım
def __init__(self, name): self.isim = isim
# Assign the argument to the instance's name attribute
self.name = name
# An instance method. All methods take "self" as the first argument # Bir metot. Bütün metotlar ilk parametre olarak "self "alır.
def say(self, msg): def soyle(self, mesaj):
return "{name}: {message}".format(name=self.name, message=msg) return "{isim}: {mesaj}".format(isim=self.name, mesaj=mesaj)
# A class method is shared among all instances # Bir sınıf metotu bütün nesnelere paylaştırılır
# They are called with the calling class as the first argument # İlk parametre olarak sınıf alırlar
@classmethod @classmethod
def get_species(cls): def getir_tur(snf):
return cls.species return snf.tur
# A static method is called without a class or instance reference # Bir statik metot, sınıf ve nesnesiz çağrılır
@staticmethod @staticmethod
def grunt(): def grunt():
return "*grunt*" return "*grunt*"
# Instantiate a class # Sınıfı çağıralım
i = Human(name="Ian") i = Insan(isim="Ahmet")
print(i.say("hi")) # prints out "Ian: hi" print(i.soyle("merhaba")) # çıktı "Ahmet: merhaba"
j = Human("Joel") j = Insan("Ali")
print(j.say("hello")) # prints out "Joel: hello" print(j.soyle("selam")) # çıktı "Ali: selam"
# Call our class method # Sınıf metodumuzu çağıraim
i.get_species() # => "H. sapiens" i.getir_tur() # => "H. sapiens"
# Change the shared attribute # Paylaşılan değeri değiştirelim
Human.species = "H. neanderthalensis" Insan.tur = "H. neanderthalensis"
i.get_species() # => "H. neanderthalensis" i.getir_tur() # => "H. neanderthalensis"
j.get_species() # => "H. neanderthalensis" j.getir_tur() # => "H. neanderthalensis"
# Call the static method # Statik metodumuzu çağıralım
Human.grunt() # => "*grunt*" Insan.grunt() # => "*grunt*"
#################################################### ####################################################