Here is my class Bar:
class Bar:
def __init__(self, start, end, open, volume, high=open, low=open, last=open):
self.start = start
self.end = end
self.open = open
self.high = high
self.low = low
self.last = last
self.volume = int(volume)
def __str__(self):
return self.start.strftime("%m/%d/%Y\t%H:%M:%S") + "\t" + self.end.strftime("%H:%M:%S") + "\t" +str(self.open) + "\t" + str(self.high) + "\t" + str(self.low) + "\t" + str(self.last) + "\t" + str(self.volume)
1) I am trying to initialize the high, low, and last for whatever open is. Is this the right way to do this?
2) When I do print(str(bar)) I get funny output such as…
03/13/2012 12:30:00 13:30:00 138.91 <built-in function open> 138.7 <built-in function open> 13177656
If you had written your function as
you would have gotten a NameError complaining that the name
foois not defined. This is because the default value for a parameter cannot refer to another parameter, which isn’t yet defined when you try to take its value. (Default values are set when the method is defined, not when it is called.)However,
openis a built-in function in Python, so it was defined; it just wasn’t theopenyou meant it to be. The right way to do this is probablyIn addition, you might want to use ‘open_’ instead of ‘open’ as the parameter name, just to avoid confusion (and temporarily shadowing) the built-in function.