1
cardPayments={}
rowNumber, NumOfCardPayments = 0, len(cardPayments)

for x in range(5):
    cardPayments.update({x:x})
    print(cardPayments)
    print('cardPayments: '+str(NumOfCardPayments)+'\n')

Output:

{0: 0}
cardPayments: 0

{0: 0, 1: 1}
cardPayments: 0

{0: 0, 1: 1, 2: 2}
cardPayments: 0

{0: 0, 1: 1, 2: 2, 3: 3}
cardPayments: 0

{0: 0, 1: 1, 2: 2, 3: 3, 4: 4}
cardPayments: 0

I was hoping that by referencing NumOfCardPayments to len(cardPayments) that as cardPayments dict increased in size so would the int value of NumOfCardPayments during the for loop, this seems to not be the case.

How can I create NumOfCardPayments as a variable which is maintained as a reference of len(cardPayments)?

(Please note, I would not like to simply do NumOfCardPayments+=1 within the for loop)

Phoenix
  • 4,386
  • 10
  • 40
  • 55
  • 5
    Why can't you just use `len(cardPayments)` each time? – Tyler Apr 03 '14 at 14:39
  • Thanks for the input Tyler, the question wasn't so much based around the easiest way to solve the problem presented but instead 'how to solve the problem in this specific way'. I agree it may, with reference to the example, not be the most simplified or efficient but it was the understanding of how to do it that I was interested in. – Phoenix Apr 03 '14 at 14:57
  • No worries, that's why I was asking. – Tyler Apr 03 '14 at 14:58

3 Answers3

4

The closest thing that comes to mind is a property, which automatically evaluates a function when read:

>>> class LenDict(dict):
...   @property
...   def len(self):
...     return len(self)
...
>>> d=LenDict()
>>> d.len
0
>>> d[1]=2
>>> d.len
1
>>>

But IMHO this is no clearer than using len(d) directly, and certainly not as efficient. It's also possible to place the hook the other way around by wrapping setitem.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
2

I don't think it is possible. In Python, variables contain pointers to objects. Integer objects are immutable. Therefore, the only way to change the value of numOfCardPayments is to bind it to a new object...

green lantern
  • 1,086
  • 7
  • 10
  • @Robert: binding a variable to a new object is done by assignment. You would need to do `NumOfCardPayments = len(cardPayments)` each time before you use `NumOfCardPayments`. It seems better to simply use `len(cardPayments)` each time. – green lantern Apr 03 '14 at 14:50
  • 1
    Another option is to replace `numOfCardPayments` with a function: `def numOfCardPayments(): return len(cardPayments)` and then use `numOfCardPayments()` – green lantern Apr 03 '14 at 14:51
  • One could even hide the function using a property. Still no more readable than just using len() in the first place. – Yann Vernier Apr 03 '14 at 14:56
  • Could you give an example of this please Yann? – Phoenix Apr 03 '14 at 14:57
1

You could make a simple lambda object to return the length of a specific object

def len_follower(target_object):
    return lambda : len(target_object)

Then set the parameter target_object as cardPayments

>>>cardPayments = {}
>>>NumOfCardPayments = len_follower(cardPayments)
>>>NumOfCardPayments()
0
>>>cardPayments[1] = 0
>>>NumOfCardPayments()
1
flakes
  • 21,558
  • 8
  • 41
  • 88