2

In C++, I could imagine constructing with a reference to a counter ( see below ), and then a function would just dereference the address to get a value. Is something similar possible in python?

Something like:

import time
class Count_Watcher:

    def __init__( self, to_watch ):

        self.to_watch = to_watch
        sleep_watch()

    def sleep_watch( self ):

        time.sleep( 5 )
        print( self.to_watch )

line_counter = 0
separate_thread_printer = Count_Watcher( (?some operator) line_counter )

for line in some_file:
    line_counter += 1

and the "current" value of line_counter ( as in, current to the for loop ) is printed every five seconds

5
  • Nope, not possible in Python. Your best bet is to make the counter an attribute of the Count_Watcher object. Commented Jun 22, 2015 at 17:55
  • You would have to pass the variable's name as a string ('line_counter'), and then you'd have to know what namespace to look it up in (e.g., the global namespace in this case). Commented Jun 22, 2015 at 17:56
  • @CoryKramer But if I make it an attribute, wouldn't I have to repeatedly update the counter in the for loop? Commented Jun 22, 2015 at 17:56
  • Aren't you doing that already? Commented Jun 22, 2015 at 17:58
  • 1
    Python is pass by value. You get a copy of the thing, not a refrence. If you pass an object (a hash or list is an object), You can check if the elements in the list has changed. ex: pass aHash and look for aHash["watchVar"]. If aHash["watchVar"] , you can tell. Commented Jun 22, 2015 at 18:03

2 Answers 2

1

A raw int will not work, but as k4vin points, out, any other type of object that can be referenced, will.

We can demonstrate this with a list that contains the count, as k4vin did:

class Watcher(object):
    def __init__(self, to_watch):
        self.to_watch = to_watch

    def print_current_value(self):
        print self.to_watch

i = 0
watcher = Watcher(i)
watcher.print_current_value()
# prints 0
i += 3
watcher.print_current_value()
# still prints 0

l = [0]
watcher = Watcher(l)
watcher.print_current_value()
# prints [0]
l[0] += 3
watcher.print_current_value()
# prints [3]

But it's a little clunky to keep your count in a list, so one option is to roll your own simple counter, which you can then reference (as with the list):

class Counter(object):
    def __init__(self):
        self.count = 0

    def __add__(self, incr):
        self.count += incr

    def __str__(self):
        return str(self.count)

c = Counter()
watcher = Watcher(c)
watcher.print_current_value()
# prints 0
c += 3
watcher.print_current_value()
# hooray! prints 3
Sign up to request clarification or add additional context in comments.

Comments

1

You can do that by wrapping the variable into a list as shown below since all the reference points to the same instance of the list

import time
class Count_Watcher:

    def __init__( self, to_watch ):

        self.to_watch = to_watch
        self.sleep_watch()

    def sleep_watch( self ):

        time.sleep( 5 )
        print( self.to_watch[0] )

line_counter = [0]
separate_thread_printer = Count_Watcher(line_counter)

for line in some_file:
    line_counter[0] += 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.