http___pythontr.blogspot.com.tr_2008_08_nesnel-programlama-sizin-programnz.html_1.xml
1.1 2.1 2.2 2.3 2.4 2.5 3.1 4.1 5.1 5.2 5.3 6.1 7.1 8.1 9.1 10.1 11.1 12.1 13.1 14.1 15.1 16.1 17.1 18.1 19.1 20.1 21.1 22.1 23.1 24.1 25.1 26.1 26.2 26.3 27.1 28.1 29.1 30.1 31.1 32.1 33.1 34.1 35.1 36.1 37.1 38.1 39.1 40.1 41.1 42.1 43.1 44.1 45.1 46.1 47.1 48.1 49.1 50.1 50.2 51.1 51.2 52.1 53.1 55.1 55.2 56.1 56.2 56.3 56.4 57.1 59.1 61.1 61.2 62.1 63.1 64.1 64.2 64.3 65.1 66.1 67.1 67.2 68.1 69.1 69.2 70.1 71.1 72.1 73.1 74.1 74.2 75.1 75.2 75.3 76.1 77.1 79.1 79.2 80.1 81.1 82.1 82.2 83.1 83.2 84.1 85.1 85.2 86.1 87.1 87.2 87.3 88.1 89.1 89.2 90.1 91.1 92.1 93.1 94.1 95.1 96.1 97.1 98.1 99.1 100.1 101.1 102.1 103.1 104.1 106.1 108.1 110.1 111.1 111.2 112.1 113.1 114.1 115.1 116.1 117.1 118.1 119.1 120.1 121.1 122.1 123.1 124.1 125.1 126.1 127.1 128.1 129.1 130.1 131.1 132.1 133.1 134.1 136.1 138.1 139.1 140.1 140.2 141.1 142.1 142.2 143.1 144.1 145.1 146.1 147.1 148.1 149.1 150.1 151.1 152.1 153.1 154.1 155.1 156.1 157.1 158.1 159.1 160.1 161.1 162.1 163.1 164.1 165.1 166.1 167.1 168.1 168.2 169.1 169.2 170.1 171.1 171.2 172.1 173.1 173.2 174.1 175.1 176.1 176.2 176.3 177.1 177.2 177.3 179.1 179.2 180.1 181.1 182.1 184.1 185.1 186.1 187.1 187.2 187.3 188.1 189.1 190.1 191.1 191.2 192.1 193.1 194.1 195.1 196.1 197.1 198.1 199.1 200.1 201.1 202.1 203.1 204.1 205.1 206.1 207.1 208.1 209.1 210.1 211.1 212.1 213.1 214.1 215.1 216.1 217.1 218.1 219.1 221.1 221.2 222.1 222.2 223.1 224.1 225.1 226.1 227.1 228.1 229.1 230.1 231.1 232.1 233.1 234.1 235.1 236.1 236.2 236.3 237.1 238.1 239.1 240.1 241.1 242.1 243.1 244.1 245.1 246.1 247.1 248.1 249.1 250.1 251.1 252.1 253.1 254.1 255.1 256.1 257.1 258.1 259.1 260.1 261.1 262.1 263.1 264.1 265.1 266.1 267.1 268.1 268.2 270.1 271.1 272.1 273.1 275.1 276.1 278.1 279.1 281.1 281.2 282.1 284.1 285.1 287.1 288.1 289.1 290.1 290.2 291.1 292.1 293.1 294.1
Classes and Objects I In python, everything is an object. Classes provide the mechanism for creating new kinds of objects. In this tutorial, we ignore the basics of classes and object oriented programming and focus on topics that provide a better understanding of object oriented programming in python. It is assumed that we are dealing with new style classes. These are python classes that inherit from object super class. Defining Classes The class statement is used to define new classes. The class statement defines a set of attributes, variables and methods, that are associated with and shared by a collection of instances of such a class. A simple class definition is given below: class Account(object): num_accounts = 0 def __init__(self, name, balance): self.name = name self.balance = balance Account.num_accounts += 1 def del_account(self): Account.num_accounts -= 1 def deposit(self, amt): self.balance = self.balance + amt def withdraw(self, amt): self.balance = self.balance - amt def inquiry(self): return self.balance Class definitions introduce the following new objects: - Class object - Instance object - Method object Class Objects When a class definition is encountered during the execution of a program, a new namespace is created and this serves as the namespace into which all class variable and method definition name bindings go. Note that this namespace does not create a new local scope that can be used by class methods hence the need for fully qualified names when accessing variables in methods. The Account class from the previous section illustrates this; methods trying to access the num_of_accounts variable must use the fully qualified name, Account.num_of_accounts else an error results as shown below when the fully qualified name is not used in the __init__ method: class Account(object): num_accounts = 0 def __init__(self, name, balance): self.name = name self.balance = balance num_accounts += 1 def del_account(self): Account.num_accounts -= 1 def deposit(self, amt): self.balance = self.balance + amt def withdraw(self, amt): self.balance = self.balance - amt def inquiry(self): return self.balance >>> acct = Account('obi', 10) Traceback (most recent call last): File "python", line 1, in File "python", line 9, in __init__ UnboundLocalError: local variable 'num_accounts' referenced before assignment At the end of the execution of a class definition, a class object is created. The scope that was in effect just before the class definition was entered is reinstated, and the class object is bound here to the class name given in the class definition header. A little diversion here, one may ask, if the class created is an object then what is the class of the class object ?. In accordance to the everything is an object philosophy of python, the class object does indeed have a class which it is created from and in the python new style class, this is the type class. >>> type(Account) So just to confuse you a bit, the type of a type, the Account type, is type. The type class is a metaclass, a class used for creating other classes and we discuss this in a later tutorial. Class objects support attribute reference and instantiation. Attributes are referenced using the standard dot syntax of object followed by dot and then attribute name: obj.name. Valid attribute names are all the variable and method names present in the class’s namespace when the class object was created. For example: >>> Account.num_accounts >>> Account.deposit Class instantiation uses function notation. Instantiation involved calling the class object like a normal functions without parameter as shown below for the Account class: >>> Account() After instantiation of a class object, an instance object is returned and the __init__ , if it has been defined in the class, is called with the instance object as the first argument. This performs any user defined initialization such as initializing instance variable values. In the case of the Account class the account name and balance are set and the number of instance objects is incremented by one. Instance Objects If class objects are the cookie cutters then instance objects are the cookies that are the result of instantiating class objects. Attribute, data and method objects, references are the only operations that are valid on instance objects. Method Objects Method objects are similar to function objects. If x is an instance of the Account class, x.deposit is an example of a method object. Methods have an extra argument included in their definition, the self argument. This self argument refers to an instance of the class. Why do we have to pass an instance as an argument to a method? This is best illustrated by a method call: >>> x = Account() >>> x.inquiry() What exactly happens when an instance method is called? You may have noticed that x.inquiry() is called without an argument above, even though the method definition for inquiry() requires the self argument. What happened to this argument? The special thing about methods is that the object on which a method is being called is passed as the first argument of the function. In our example, the call to x.inquiry() is exactly equivalent to Account.f(x). In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method’s object before the first argument. The python tutorial says: When an instance attribute is referenced that isn’t a data attribute, its class is searched. If the name denotes a valid class attribute that is a function object, a method object is created by packing (pointers to) the instance object and the function object just found together in an abstract object: this is the method object. When the method object is called with an argument list, a new argument list is constructed from the instance object and the argument list, and the function object is called with this new argument list. The above applies to all instance method object including the __init__ method. The self argument is actually not a keyword and any valid argument name can be used as shown in the below definition for the Account class: class Account(object): num_accounts = 0 def __init__(obj, name, balance): obj.name = name obj.balance = balance Account.num_accounts += 1 def del_account(obj): Account.num_accounts -= 1 def deposit(obj, amt): obj.balance = obj.balance + amt def withdraw(obj, amt): obj.balance = obj.balance - amt def inquiry(obj): return obj.balance >>> Account.num_accounts >>> x = Account('obi', 0) >>> Account.inquiry(x) Static and Class Methods All methods defined in a class by default operate on instances. However, one can define static or class methods by decorating such methods with the corresponding @staticmethod or @classmethod decorators. Static Methods Static methods are normal functions that exist in the namespace of a class. Referencing a static method from a class shows that rather than an unbound method type, a function type is returned as shown below: class Account(object): num_accounts = 0 def __init__(self, name, balance): self.name = name self.balance = balance Account.num_accounts += 1 def del_account(self): Account.num_accounts -= 1 def deposit(self, amt): self.balance = self.balance + amt def withdraw(self, amt): self.balance = self.balance - amt def inquiry(self): return "Name={}, balance={}".format(self.name, self.balance) @staticmethod def type(): return "Current Account" >>> Account.deposit >>> Account.type To define a static method, the @staticmethod decorator is used and such methods do not require the self argument. Static methods provide a mechanism for better organization as code related to a class are placed in that class and can be overridden in a sub-class as needed. Class Methods Class methods as the name implies operate on classes themselves rather than instances. Class methods are created using the @classmethod decorator with the class rather than instance passed as the first argument to the method. import json class Account(object): num_accounts = 0 def __init__(self, name, balance): self.name = name self.balance = balance Account.num_accounts += 1 def del_account(self): Account.num_accounts -= 1 def deposit(self, amt): self.balance = self.balance + amt def withdraw(self, amt): self.balance = self.balance - amt def inquiry(self): return "Name={}, balance={}".format(self.name, self.balance) @classmethod def from_json(cls, params_json): params = json.loads(params_json) return cls(params.get("name"), params.get("balance")) @staticmethod def type(): return "Current Account" A motivating example of the usage of class methods is as a factory for object creation. Imagine data for the Account class comes in different formats such as tuples, json string etc. We cannot define multiple __init__ methods as a Python class can have only one __init__ method so class methods come in handy for such situations. In the Account class defined above for example, we want to initialize an account from a json string object so we define a class factory method, from_json that takes in a json string object and handles the extraction of parameters and creation of the account object using the extracted parameters. Another example of a class method in action is the dict.fromkeys methods that is used for creating dict objects from a sequence of supplied keys and value. Python Special Methods Sometimes we may want to customize user-defined classes. This may be either to change the way class objects are created and initialized or to provide polymorphic behavior for certain operations. Polymorphic behavior enables user-defined classes to define their own implementation for certain python operation such as the + operation. Python provides special methods that enable this. These methods are normally of the form * refers to a method name. Examples of such methods are __init__ and __new__ for customizing object creation and initialization, __getitem__, __add__ and __sub__ for emulating in built python types, __getattribute__, __getattr__ etc. for customizing attribute access etc. These are just a few of the special methods. We discuss a few important special methods below to provide an understanding but the python documentation provides a comprehensive list of these methods. Special methods for Object Creation New class instances are created in a two step process using the __new__ method to create a new instance and the __init__ method to initialize the newly created object. Users are already familiar with defining the __init__ method; the __new__ method is rarely defined by the user for each class but this is possible if one wants to customize the creation of class instances. Special methods for Attribute access We can customize attribute access for class instances by implementing the following listed methods. class Account(object): num_accounts = 0 def __init__(self, name, balance): self.name = name self.balance = balance Account.num_accounts += 1 def del_account(self): Account.num_accounts -= 1 def __getattr__(self, name): return "Hey I dont see any attribute called {}".format(name) def deposit(self, amt): self.balance = self.balance + amt def withdraw(self, amt): self.balance = self.balance - amt def inquiry(self): return "Name={}, balance={}".format(self.name, self.balance) @classmethod def from_dict(cls, params): params_dict = json.loads(params) return cls(params_dict.get("name"), params_dict.get("balance")) @staticmethod def type(): return "Current Account" x = Account('obi', 0) __getattr__(self, name)__: This method is only called when an attribute, name, that is referenced is neither an instance attribute nor is it found in the class tree for the object. This method should return some value for the attribute or raise an AttributeErrorexception. For example, if x is an instance of the Account class defined above, trying to access an attribute that does not exist will result in a call to this method. >>> acct = Account("obi", 10) >>> acct.number Hey I dont see any attribute called number Note that If __getattr__code references instance attributes, and those attributes do not exist, an infinite loop may occur because the __getattr__method is called successively without end. __setattr__(self, name, value)__: This method is called whenever an attribute assignment is attempted. __setattr__should insert the value being assigned into the dictionary of the instance attributes rather than using self.name=valuewhich results in a recursive call and hence to an infinite loop. __delattr__(self, name)__: This is called whenever del objis called. __getattribute__(self, name)__: This method is always called to implement attribute accesses for instances of the class. Special methods for Type Emulation Python defines certain special syntax for use with certain types; for example, the elements in lists and tuples can be accessed using the index notation [], numeric values can be added with the + operator and so on. We can create our own classes that make use of this special syntax by implementing certain special methods that the python interpreter calls whenever it encounters such syntax. We illustrate this with a very simple example below that emulates the basics of a python list. class CustomList(object): def __init__(self, container=None): # the class is just a wrapper around another list to # illustrate special methods if container is None: self.container = [] else: self.container = container def __len__(self): # called when a user calls len(CustomList instance) return len(self.container) def __getitem__(self, index): # called when a user uses square brackets for indexing return self.container[index] def __setitem__(self, index, value): # called when a user performs an index assignment if index <= len(self.container): self.container[index] = value else: raise IndexError() def __contains__(self, value): # called when the user uses the 'in' keyword return value in self.container def append(self, value): self.container.append(value) def __repr__(self): return str(self.container) def __add__(self, otherList): # provides support for the use of the + operator return CustomList(self.container + otherList.container) In the above, the CustomList is a thin wrapper around an actual list. We have implemented some custom methods for illustration purposes: __len__(self): This is called when the len()function is called on an instance of the CustomListas shown below: >>> myList = CustomList() >>> myList.append(1) >>> myList.append(2) >>> myList.append(3) >>> myList.append(4) >>> len(myList) 4 __getitem__(self, value): provides support for the use of square bracket indexing on an instance of the CustomList class as shown below: >>> myList = CustomList() >>> myList.append(1) >>> myList.append(2) >>> myList.append(3) >>> myList.append(4) >>> myList[3] 4 __setitem__(self, key, value): Called to implement the assignment of value to to self[key] on an instance of the CustomList class. >>> myList = CustomList() >>> myList.append(1) >>> myList.append(2) >>> myList.append(3) >>> myList.append(4) >>> myList[3] = 100 4 >>> myList[3] 100 __contains__(self, key): Called to implement membership test operators. Should return true if item is in self, false otherwise. >>> myList = CustomList() >>> myList.append(1) >>> myList.append(2) >>> myList.append(3) >>> myList.append(4) >>> 4 in myList True __repr__(self): Called to compute the object representation for self when print is called with self as argument. >>> myList = CustomList() >>> myList.append(1) >>> myList.append(2) >>> myList.append(3) >>> myList.append(4) >>> print myList [1, 2, 3, 4] __add__(self, otherList): Called to compute the addition of two instances of CustomList when the +operator is used to add two instances together. >>> myList = CustomList() >>> otherList = CustomList() >>> otherList.append(100) >>> myList.append(1) >>> myList.append(2) >>> myList.append(3) >>> myList.append(4) >>> myList + otherList + otherList [1, 2, 3, 4, 100, 100] The above provide an example of how we can customize class behavior by defining certain special class methods. For a comprehensive listing of all such custom methods, see the python documentation. In a follow-up tutorial, we put all we have discussed about special methods together and explain descriptors, a very important functionality that has widespread usage in python object oriented programming. Further Reading Python Essential Reference Python Data Model
1.1 2.1 3.1 4.1 5.1 6.1 7.1 8.1 9.1 10.1 11.1 12.1 13.1 14.1 15.1 16.1 17.1 18.1 19.1 20.1 21.1 22.1 23.1 24.1 25.1 26.1 27.1 28.1 29.1 30.1 31.1 32.1 33.1 34.1 35.1
- Nesnel yaklaşımda bir sınıf yaratılır, sınıf bir genellemedir. Belli bir grup nesnenin ortak özelliklerini barındırır(örn: organism sınıfı bütün organizmalarda bulunan işlem ve özellikleri barındırır.) - Nesne bu sınıf'ın bir nesneye uygulanmasıdır animal = organism() yada human = organism() dediğimiz zaman buradaki animal ve human birer nesnedir. - Sınıf özellikleri bu sınıfa ait özelliklerdir. Örneğin her organizmanın bir yaşı vardır. Bu yaş bigisini organism sınıfı üzerinde age olarak tanımlarsak [1] daha sonra bu sınıfa ait nesnelerin age özelliğini animal.age şeklinde çağırabiliriz. - Bir sınıf kendi özelliklerini kendi kendine de değiştirebilir. Bu durumda self.ozellik = hede şeklinde kullanılır. Buradaki aslında self sınıfın kendisini ifade eder. Eğer bir fonksiyon içerisinde self. kullanılacaksa o fonksiyonun argümanı içerisinde self bulumalıdır.[2] - Bir nesne oluşturuluyorken __init__ fonksiyonu otomatik olarak çağırılır. - Bir nesne siliniyorken __del__ fonksiyonu otomatik olarak çağırılır. Eğer del(nesne) kullanılarak nesne silinmez ise nesne program sonunda silinir. - __init__ ve __del__ fonksiyonları self argümanı ile belirtilmek zorundadır. - örnek olarak bir organizma sınıfı şu şekilde tanımlanabilir. class organism: def __init__(self): print "ben doğdum" self.age = 0 [1] def grow_up(self,years): self.age += years def __del__(self): print "ben öldüm" animal = organism() print "animal age is :",animal.age animal.grow_up(3) # grow up 3 years print "animal age is :", animal.age 14 Ağustos 2008 Perşembe Pyton ve Nesnel Programlama 5 yorum: girintiye dikkat!.. malum python kodluyorsunuz. :)YanıtlaSil opppsss :) düzelttim uyarı çin teşekkürler.YanıtlaSil self.age = 0 [1]YanıtlaSil Buradaki [1] fazladan sanırım :) Ayrıca, sınıf içerisinde tanımladığımı tüm fonksiyonlara "self" parametresi göndermek zorunda mıyız? Eğer öyleyse -ki öyle görünüyor- bu saçma değil mi? Orada ki self şu işe yarıyor.YanıtlaSil self.age=0 diyorsan pythona diyorsun ki ben buna dışarıdan ulaşacam bana yer ayır. kopek=organizma() kopek.age derken kopek self ile ayırmış olduğun yere geçiyor. Yani self kullanmazsan dışarıdan erişemezsin. Umarım karıştırmamışımdır :S http://www.freenetpages.co.uk/hp/alan.gauld/tutdata.htm#class şuradaki örnekleride incelersen diğer dillerde de benzer kullanımlar olduğunu göreceksin.YanıtlaSil