Class Counter
object --+
|
Counter
This class was originally designed for the convenience of gridding
widgets. For example, instead of writing a code like:
my_label1.grid( row = 1, sticky = W )
my_label2.grid( row = 2, sticky = W )
my_label3.grid( row = 3, sticky = W )
we can avoid the hardcoding ('row = 1', etc., which is generally bad
and hard to maintain) using this class. The improved code will look
like:
row_index = Counter()
my_label1.grid( row = row_index.val, sticky = W )
my_label2.grid( row = row_index.val, sticky = W )
my_label3.grid( row = row_index.val, sticky = W )
which is equivalent to the above code, but generally easier to write
and modify. The trick is that the property 'val', when read, will return
the current value of the internal counter and then increment the counter
(not the returned value) by 1.
If the user just wants to get the current value of the counter but not
want to change it, s/he can do either one of the two:
-
Use the 'va_' property,
-
Explicitly convert the object to 'int'.
|
__init__(self,
val=0)
Constructs the object. |
|
|
|
__int__(self)
Supports conversion to an integer. |
|
|
|
__cmp__(self,
other)
Supports comparisons with integers or objects convertible to
integers. |
|
|
|
reset(self,
val=0)
Resets the counter to 'val'. |
|
|
|
|
|
|
Inherited from object :
__delattr__ ,
__format__ ,
__getattribute__ ,
__hash__ ,
__new__ ,
__reduce__ ,
__reduce_ex__ ,
__repr__ ,
__setattr__ ,
__sizeof__ ,
__str__ ,
__subclasshook__
|
__init__(self,
val=0)
(Constructor)
|
|
Constructs the object. One can provide a value 'val' to initialize the
internal variable. For example:
row_index = Counter( 2 )
will let the counter start from 2, instead of 0 (default value).
- Overrides:
object.__init__
|
val
Readonly. When read, this returns the value of the current count and
then increment the count by 1. The incrementation does not affect the
returned value.
- Get Method:
- __get_val(self)
|
va_
Readonly. When read, this returns the value of the current count
without changing the internal state whatsoever of the object.
- Get Method:
- __get_va_(self)
|