User Tools

Site Tools


python:python_misc:oo_fundamentals_with_python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
python:python_misc:oo_fundamentals_with_python [2018/08/20 20:15] – ↷ Page name changed from python:python_misc:python_oo_fundamentals to python:python_misc:oo_fundamentals_with_python mithatpython:python_misc:oo_fundamentals_with_python [2018/08/20 20:24] mithat
Line 98: Line 98:
   * //reset//: sets the count to zero.   * //reset//: sets the count to zero.
  
-Next we can think about what data we'll need to keep track of the state of a clicker-counter. In this case, it's pretty simple: all we really need is one integer to store the count value.+Next we can think about what data we'll need to keep track of the state of a clicker-counter. In this case, it's pretty simple: all we really need is one integer to store the //count// value.
  
 So, a summary of what we need so far is: So, a summary of what we need so far is:
Line 104: Line 104:
   * a //click// operation   * a //click// operation
   * a //reset// operation   * a //reset// operation
-  * an integer to store the count+  * an integer to store the //count//
  
 In Python, object **attributes**, which together make up the state, are defined in special variables called **instance variables**. A class definition can include as many instance variables as it needs to store the state. In our case, we are getting off easy: the clicker-counter only needs one. The **operations** our object will be capable of, which make up behavior, are defined using **instance methods**. In Python a method is nothing more than a function defined inside a class. We call anything belonging to a class (e.g., instance variables and methods) a **member** of the class. In Python, object **attributes**, which together make up the state, are defined in special variables called **instance variables**. A class definition can include as many instance variables as it needs to store the state. In our case, we are getting off easy: the clicker-counter only needs one. The **operations** our object will be capable of, which make up behavior, are defined using **instance methods**. In Python a method is nothing more than a function defined inside a class. We call anything belonging to a class (e.g., instance variables and methods) a **member** of the class.
Line 126: Line 126:
 </code> </code>
  
-Class definitions follow the same header/suite pattern for compound statements you've seen before with control flow statements and function definitions. The keyword ''class'' in the header declares that what follows is a class definition. The 'ClickerCounter' identifier is the name of our class. And the parenthesis are there for implementing an advanced feature that we'll not tackle here.+Class definitions follow the same header/suite pattern for compound statements you've seen before with control flow statements and function definitions. The keyword ''class'' in the header declares that what follows is a class definition. The ''ClickerCounter'' identifier is the name of our class. The parenthesis that follow the name of the class are there for implementing an advanced feature that we'll not tackle here.
  
-The suite of the class definition nests additional compound statements, in this case a set of function definitions---the instance methods.+The suite of the class definition nests additional compound statementsfunction definitions that make up the instance methods.
  
 In this class definition, we define three instance methods: ''%%__init__(self)%%'', ''click(self)'', and ''reset(self)''. These methods will belong to objects created with this class; they won't have any meaning outside of this context. In this class definition, we define three instance methods: ''%%__init__(self)%%'', ''click(self)'', and ''reset(self)''. These methods will belong to objects created with this class; they won't have any meaning outside of this context.
Line 135: Line 135:
  
 <WRAP center round tip 80%> <WRAP center round tip 80%>
-A common error is to forget to use ''self'' as the first parameter in an instance method definition. Another common error is to forget to use ''self'' to qualify the names of instance variables. Things will go wonky if you do either of these.+A common error is to forget to use ''self'' as the first parameter in an instance method definition. Another common error is to forget to use ''self'' to qualify the names of instance variables inside instance methods. Things won'go as expected if you do either of these.
 </WRAP> </WRAP>
  
python/python_misc/oo_fundamentals_with_python.txt · Last modified: 2018/11/30 18:45 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki