GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — learn-python/src/classes/test_method_objects.py at master · NuttyCoder/learn-python Expand file tree/ Copy pathtest_method_objects.pyMore file actions 60 lines (45 loc) · 2.63 KB / Copy pathtest_method_objects.pyFile metadata and controls60 lines (45 loc) · 2.63 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 """Class Definition Syntax. @see: https://docs.python.org/3/tutorial/classes.html#method-objects Classes can have two types of attribute references: data or methods. Class methods are called by [variable_name].[method_name]([parameters]) as opposed to class data which lacks the (). """ class MyCounter: """A simple example of the counter class""" counter = 10 def get_counter(self): """Return the counter""" return self.counter def increment_counter(self): """Increment the counter""" self.counter += 1 return self.counter def test_method_objects(): """Method Objects.""" # The other kind of instance attribute reference is a method. A method is a function that # “belongs to” an object. (In Python, the term method is not unique to class instances: other # object types can have methods as well. For example, list objects have methods called append, # insert, remove, sort, and so on. However, in the following discussion, we’ll use the term # method exclusively to mean methods of class instance objects, unless explicitly stated # otherwise.) # But be aware that counter.get_counter() is not the same thing as MyCounter.get_counter() — # it is a method object, not a function object. # Usually, a method is called right after it is bound counter = MyCounter() assert counter.get_counter() == 10 # However, it is not necessary to call a method right away: counter.get_counter() is a method # object, and can be stored away and called at a later time. For example: get_counter = counter.get_counter assert get_counter() == 10 # What exactly happens when a method is called? You may have noticed that counter.get_counter() # was called without an argument above, even though the function definition for get_counter() # specified an argument (self). What happened to the argument? Surely Python raises an # exception when a function that requires an argument is called without any — even if the # argument isn’t actually used… # Actually, you may have guessed the answer: the special thing about methods is that the # instance object is passed as the first argument of the function. In our example, the call # counter.get_counter() is exactly equivalent to MyCounter.get_counter(counter). 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 instance object # before the first argument. assert counter.get_counter() == 10 assert MyCounter.get_counter(counter) == 10 |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |