matel
matel

Reputation: 485

python undefined variable

Why the variable freq is not defined in the method update? I am calling the method candle on init and this method contains freq?

class candle:
    def __init__(self):
        self.freq = None
        self.open_price = None
        self.high_price = None
        self.low_price = None
        self.last_price = None
        self.volume = 0

    def addTick(self,tick):
        if self.open_price is None:
            self.open_price = tick.price
        if self.high_price is None or tick.price >self.high_price:
            self.high_price = tick.price
        if self.low_price is None or tick.price <self.low_price:
            self.low_price = tick.price   
        self.last_price = tick.price
        self.volume = self.volume +1


    def update(self,tick):
        self.candle.addTick(tick)
        if keyfunc(current_time,freq) != reference_timestamp[freq]:
            self.notify(self.candle)
            self.candle = candle()

Upvotes: 3

Views: 5439

Answers (2)

Meowrice
Meowrice

Reputation: 21

You're trying to access a local variable (which is not defined in that scope) instead of class instance attribute. Try self.freq instead of freq.

Upvotes: 2

unwind
unwind

Reputation: 400029

There is no implicit self in Python, you must be explicit when accessing member variables.

if keyfunc(current_time,freq) != reference_timestamp[freq]:

should be

if keyfunc(current_time, self.freq) != reference_timestamp[self.freq]:

not sure where reference_timestamp is defined, assuming it's global.

Upvotes: 2

Related Questions