I need to increment a variable each time a certain number of calls to a certain function is made in Python 2.7.3.
For example, feetTraveled = 0. for every 12 calls to travelOneInch(), feetTraveled += 1.
Is it optimal to use a for loop or should I be using something different?
If it is optimal to use a for loop like the one above, what should be placed instead of 'every 12 calls to'?
Another example: I have four servants. They each do one task in a clock tick, and the time counter is initially time = 0. So for every four calls to servantDo(), time += 1.
In this case, I think I'd use an instance method:
class Foo(object):
def __init__(self):
self.inches = 0
self.feet = 0
def travel_one_inch(self):
self.inches += 1
if self.inches > 11:
self.inches = 0
self.feet += 1
def __str__(self):
return "{feet} feet and {inches} inches".format(feet=self.feet,inches=self.inches)
a = Foo()
for _ in range(123):
a.travel_one_inch()
print(a)
A vast majority of the time, if you want a function to remember some sort of state between calls, you're not looking for a function at all -- You're looking for a class with an instance method.
If you want to work in the global namespace:
inches = 0
feet = 0
def travelOneInch(sure):
inches += 1
if (inches % 12) == 0:
feet += 1
#...
And using inches % 12 == 0 is better than inches > 12 in case you want your function to recognize the second and furher feet.
mgilson (above) knows what's up. If I were you, I'd do the following, to make things easier:
class Foo(object):
def __init__(self):
self.__inches = 0
def travel_one_inch(self):
self.__inches += 1
def feetTraveled(self):
return self.__inches // 12
But, in terms of optimality, if you're just doing this once, then you can use temporary variables:
feetTraveled = 0
inchesTraveled = 0
for i in xrange(72):
# as in mgilson's answer
inchesTraveled += 1
if inchesTraveled > 11:
inchesTraveled = 0
feetTraveled += 1
Probably the best implementation is to use an object to store the status and use the function to modify it:
class Position(object):
def __init__(self):
self.pos = 0
def advance(self,inches=1):
self.pos += inches
def pos_as_foot(self):
return self.pos // 12
If you find this approach a little to boring (even if it's the most correct one), you can try a more esoteric approach: you can use internal values of the function, i.e. treating it like an object
def foo():
foo.value+=1
if foo.value and not foo.value%12:
foo.result+=1
foo.value=0
foo.result=0
for i in range(15):
foo()
print foo.value
#15
print foo.result
#1
It's not the most easy solution, and have problem if you need several counters, but it's interesting to know. It has the advantage to remove some cluttering code (at least in simple cases) from the class definition (that is using under the hood), and keep the state accessible whereever you will use the function, without tampering with global variables (that i reaaaly don't like)
Related
So i have the Function Ball() for example. and in that Function i have imported a list with Ball names where i randomly choose one and return that name.
so i want to know how i can count how many times the Function was accessed.
i tried this:
def Ball():
counter = 0
Ball_name = Ball.man
Ball_name = random.choice(Ball_name)
counter = counter+1
return Ball_name
counter_1 = Ball(counter)
print(counter_1)
but it didnt work.
Thank you for all youre help i really.
But i dont want any answers which say, that i have to learn Python from the beging or something.
I want real help because im really stressed because of that.
I cant use Global variables
THANK YOU
You can give your function an attribute, like count, and increment it every time the function is called.
def Ball():
if not hasattr(Ball, 'count'):
Ball.count = 0
Ball.count += 1
Ball()
print(Ball.count) # will print 1
Ball()
print(Ball.count) # will print 2
You can take a look at PEP 232 -- Function Attributes if you want to know more about it.
If you have just one function where you want to count, then hacking in a function attribute will do the trick.
For a more general solution, you can use decorators:
def count(f):
def f_with_counter(*args, **kwargs):
if not hasattr(f_with_counter, 'count'):
f_with_counter.count = 0
f_with_counter.count += 1
return f_with_counter
#count
def Ball():
pass
#whatever it is that Ball does
Ball()
print(Ball.count) # should print 1 just as in the example above
Ball()
print(Ball.count) # should print 2 now.
I am a bit confused over this.
I have a function. Inside that function it asks a number of questions, one is the following based on the number of gardens they have - so if they say they have 2 gardens it will ask this question twice and should add the 100 twice to the calculation:
gardens = int(input("How many gardens do you have: "))
def gard():
calc = 0
gardener = input("Do you need a gardener? Y or N ")
if gardener == "Y" or gardener == "y":
calc = calc + 100
else:
calc = calc + 0
for i in range(gardens):
gard()
How do I keep a running total outside of the function? When I put print(calc) inside the function it just displays 100 each time they say Y but doesn't add it together.
Edited to include updated code:
The eMake section (the IF statement) returns a value - but it only ever returns the first in the calculation at the end?
Also struggling to do the area section since there are numerous ws. It only stores the last value for the variable.
noGard = int(input("Enter number of gards which require cleaning: "))
#Defining variables
Calc = 0
Area = 0
emCalc = 0
#Room information
def GInfo():
global Calc
global Area
gName = input("Enter gard name: ")
noW = int(input("How many w are in the "+gName + "? "))
#Repeats the questions for each W
for i in range(noW):
Height = float(input("What is the w height of in metres? "))
Width = float(input("What is the w width in metres? "))
Area = Height * Width
#Asks if w needs to be removed
w = input("Does w need removing? Y or N ")
if w == "Y" or w == "y":
Calc = Calc + 70
else:
Calc = Calc + 0
print (" ")
#Returns the values
return Calc
return Area
#Calculate Sarea
#Identifying e
def e():
global emCalc
#eMake
eMake = input("What make of e - HH or NN? ")
if eMake == "HH" or "hh":
emCalc = emCalc + 200
elif eType == "NN" or "nn":
emCalc = emCalc + 50
else: print("You have entered an invalid e make")
#Returns the values
return emCalc
#Repeats the g information questions for each g
for i in range(noGard):
GInfo()
# Runs the E function
e()
#Print total without VAT
total = Calc + emCalc
print(total)
print(Area)
Your function should return the calculated value.
def gard():
...
return calc
total = 0
for _ in range(gardens):
total += gard()
print 'Total: ', total
The whole point of functions, really, is that they take parameters and return values. (Some languages, although not Python, refer to functions that don't do this as "procedures".)
That is what you need to do here: your gard function needs to return the value of calc. You probably don't want to actually do the addition inside the function itself, but if you did, you would also need to accept the current value of calc as a parameter, which you would pass in from your for loop.
Functions, in the strictest sense, do not have state. When writing functional programs, one typically aims to keep their functions pure, meaning that the result of the function does not depend on anything but its inputs and does not cause observable side effects.
But Python is not a purely functional language. It is an object-oriented procedural language which models functions as objects, and objects can be stateful. So you can do what you're aiming to, if you don't take the word "function" too literally.
The Right Thing™
Create a class which models your data and the operations on it:
>>> class F(object):
... def __init__(self):
... self.x = 0
... def f(self):
... self.x += 1
... return self.x
...
>>> my_f = F()
>>> my_f.f()
1
>>> my_f.f()
2
Fun and naughty ways
Add state to the function object, taking advantage of the fact that function bodies aren't executed until the function is called:
>>> def f():
... f.x += 1
... return f.x
...
>>> f.x = 0
>>> f()
1
>>> f()
2
If you want to do this transparently (that is, make it so that you don't have to add this state to the function right after defining it) you can close over the state by having a function create a function:
>>> def g():
... def func():
... func.x += 1
... return func.x
... func.x = 0
... return func
...
>>> f = g()
>>> f()
1
>>> f()
2
To take it a step further, create a decorator so you don't have to do any assignments after defining the function at all:
>>> def with_x(func):
... func.x = 0
... return func
...
>>> #with_x
... def f():
... f.x += 1
... return f.x
...
>>> f()
1
>>> f()
2
Or you can just use global to let a function refer to something outside of its local scope, not taking advantage of the fact that functions are objects:
>>> x = 0
>>> def f():
... global x
... x += 1
... return x
...
>>> f()
1
>>> f()
2
>>> x
2
Updates for your edit
Since you went with global I'll first refer you to a good question that explains global. Using global variables in a function other than the one that created them
Now, as for your particular problems:
The eMake section (the IF statement) returns a value - but it only ever returns the first in the calculation at the end?
Sure, there are a couple problems here and one of them is definitely a common one for beginners. or takes higher precedence than == so your condition parses like this:
if (eMake == "HH") or ("hh"):
This gets people all the time. In Python, if a value isn't a boolean and you put it in a conditional statement, it gets evaluated as a boolean using a series of truthiness rules. In this case a non-empty string is considered True so you are basically saying if (eMake == "HH") or True.
To fix this, fix the right-hand side of the condition:
if (eMake == "HH") or (eMake == "hh"):
By the way, you probably meant elif (eMake == "NN") or (eMake == "nn"): instead of elif eType == "NN" or "nn": because you never defined eType (and for the reason above, too.) If you type nn there you'll get an exception.
Also struggling to do the area section since there are numerous ws. It only stores the last value for the variable.
This is because you repeatedly assign to the same variable with Area = Height * Width. Since Area is global, it's the same variable every time you call GInfo(). If it wasn't global it would be a new variable every time you called the function, but then you would need to return it and assign the return value to a variable in order to save the value. Otherwise it would disappear since it was never assigned to anything.
Now, I don't know what you are trying to do with the areas you're calculating. Do you want to keep them separate or sum them together?
If you want to keep them separate, you'll need to use a data structure. In this case, you'd definitely want to use a list. Using the append() method of lists, you can add an item to the list. So it would look something like this:
areas = [] # empty list
def GInfo():
global areas
# the stuff before the loop
for i in range(noW):
Height = float(input("What is the w height of in metres? "))
Width = float(input("What is the w width in metres? "))
areas.append(Height * Width)
# the stuff after the loop
If you want to sum them together, just make sure you add each individual area calculation to the previous result, just like you did with Calc:
Area += Height * Width
One more thing: your GInfo() function is only returning Calc and not Area as well. Functions can only return one value. In the mathematical sense, a function is a many-to-one mapping between two sets. So in Python, a function ends at the return statement. Nothing else gets executed after that.
In order to get both the value of Calc as well as the value of Area from the return value of GInfo(), you will have to return a data structure. Usually this would be a tuple.
return (Calc, Area)
But your code doesn't assign the return value of GInfo() to anything. Instead, it uses the global declaration to change the value of the global variables. So there shouldn't be an issue here.
I have written a function to calculate the heading between two points only if a vehicle reports that it's moving and that the vehicle has moved 20cm between points.
The function uses static variables - or at least it would if it worked - to keep track of previous positions and heading values.
Here is the code:
def withCan(pos):
eastdist = pos[0]-previous_pos[0]
northdist = pos[1]-previous_pos[1]
canflag = pos[2]
if (canflag == 1 or canflag==2):
if (previous_canflag == 1 and canflag == 2):
previous_heading += 180.0
previous_canflag = canflag
elif (previous_canflag == 2 and canflag == 1):
previous_heading += 180.0
previous_canflag = canflag
else:
previous_canflag = canflag
if ( (canflag == 1 or canflag == 2) and math.sqrt(northdist*northdist+eastdist*eastdist) > canstep ):
previous_heading = math.degrees(math.atan2(eastdist, northdist))
previous_pos[0] = pos[0]
previous_pos[1] = pos[1]
return previous_heading
withCan.previous_pos = [0.0,0.0]
withCan.previous_heading = 0.0
withCan.previous_canflag = 0
withCan.canstep = 0.2
positions = backandforth([100,100]) #populates an array of form [x,y,canflag]
for p in positions:
print withCan(p)
I am getting an error that says eastdist = pos[0]-previous_pos[0]
NameError: global name 'previous_pos' is not defined. Please could someone explain the cause of this error?
When you do this:
def foo():
pass
foo.name = 1
You are not creating a global name name. Instead you are adding a property to the foo function! You can access it with:
def foo():
return foo.name
foo.name = 1
But that is quite weird. If you need a global name, just do it:
def foo():
global name
name += 1
return name
name = 1
Remember that if you want to modify the global name from the function, you have to declare it as global. If you fail to do this, you can use it but you cannot assign to it.
Your confusion with static names may come from using classes. But note that in your code withCan is not a class, it is a plain function!
It looks like what you are trying to do is writing a class...
class WithCan():
def __init(self, previous_pos)__:
self.previous_pos=previous_pos
def withCan(self, pos):
# your function as class method
Then you could initialize an instance
withCan=WithCan(previous_pos)
and access it
withCan.previous_pos=...
You can do static variables in Python using function attributes, but you need to use the full name inside the function to access those attributes.
Here's a short demo.
def test(a):
print a, a + test.b
test.b += 1
test.b = 5
test(3)
test(10)
output
3 8
10 16
However, it would be more usual to do this sort of thing using a class, as shown in Tim's answer.
Another way to do statics in Python is to give your function default mutable arguments, but many people are uncomfortable with that. But if you're curious, please see “Least Astonishment” in Python: The Mutable Default Argument.
Let me contribute a perhaps more streamlined way of emulating static variables in functions that could make the OP's example maybe easier to read:
def with_can(pos):
if not hasattr(with_can, "canflag"):
# set up and initialise the static variables
with_can.canflag = 0
with_can.previous_pos = [0.0,0.0]
with_can.previous_heading = 0.0
with_can.canstep = 0.2
# ... use them ...
eastdist = pos[0]-with_can.previous_pos[0]
# ... etc ...
Basically at the first invocation we detect that one of the "static" variables (canflag) is not yet there so we add and initialise all of them. After that they can be used as indicated.
However, as others have pointed out already, it is much better to write a class with data members instead of these "static" function variables.
I'm new to python and am trying to create a program to test some methods of object creation. Currently, I'm writing a program that involves creating objects, giving them a unique numeric variable, and assigning them to a list for future referencing. Here's what I wrote to create the variable names:
def getRectangleName():
rectName = list("Rectangle")
SPAWNEDOBJECTLIST.append(len(SPAWNEDOBJECTLIST))
rectName.append(str(len(SPAWNEDOBJECTLIST)))
return rectName
and then that's passed onto something to turn that string into a variable name. I tried eval(), learned this was Bad for some reason and it didn't work anyway, and tried some workarounds to no avail.
I figure there's plenty of games that have an indefinite number of characters on the screen. Is there an established way of making iterations of objects like this?
The objects themselves have an X and Y so that they act as reference points for the display of rectangles on screen(the idea in the future is to have each one move around on their own, so simply making lists of X and Y to draw rectangles isn't useful).
Edit: The problem is that I don't know how to give each object its own variable to put it on a list for future referencing.
Edit2: I don't think I'm asking the right question, actually, or using the right terminology. I need to be able to have an indefinite number of objects created on the fly after the program is already running, and be able to reference them individually.
The problem is that I don't know how to give each object its own variable to put it on a list for future referencing.
Whenever you think you need variables you didn't type into your program, you're doing something wrong. You don't need to assign something to a variable to put it on a list:
x = [1, 2, 3] # Note how I don't assign 1, 2, or 3 to variables.
x.append(4) # 4 doesn't get a variable either.
x.append(make_a_rectangle()) # We create a rectangle and stick it on the list.
do_stuff_with(x[4]) # We pass the rectangle to a function.
x = [] # New list.
for i in xrange(n):
x.append(make_a_rectangle()) # This happens n times.
# At this point, we have n rectangles, none of them associated with their own
# variable, none of them with a name.
If you think you need names for things (and quite often, you don't really need the names), you can use a dict:
x = {}
x['foo'] = make_a_rectangle()
do_stuff_with(x['foo'])
It's not a great idea to combine the function of managing the rectangles -- accessing, adding, or deleting them -- with the idea of being rectangles. You never know when you might need to maintain multiple lists, or change from unordered lists to organized ones.
Until you really need more, keep the management functions simple: use built-in lists or dictionaries. Use lists if you just care about ordering, or only need to know you have a bunch of stuff:
class Rectangle (object):
def __init__(self, top, bottom, left, right):
self.Top = top
self.Left = left
self.Right = right
self.Bottom = bottom
list_of_rects = [Rectangle(10,0,0,10), Rectangle(20, 10, 10 ,20)]
# how many rects?
len(list_of_rects)
# result: 2
# where is this particular rect?
fred = Rectangle(30,20,20, 30)
list_of_rects.insert(fred, 1)
list_of_rects.index(fred)
# result: 1
#remove an item from the list:
list_of_rects.remove(fred)
#search the list:
right_of_5 = [rect for rect in list_of_rects if rect.Left > 5]
If you need to get access to the individual rects for some reason -- 'what's the rectangle of the goal' or something -- you have two choices:
1) the code that needs the rect just keeps a reference to it:
class Goal(object):
def __init__(self, rect):
self.Rect = rect
goalrect = Rectangle (0,0,20,20)
mygoal = Goal(goalrect)
list_of_rects.append(goalrect)
# now goalrect always knows about it's own rect, but the list can keep track of it too...
2) Or, use a dictionary:
named_rects = {}
named_rects['goal'] = Rectangle(0,0,20,20)
You get all the same abilities with a dictionary that you do with a list -- add, delete, and find -- except dictionaries don't preserve order, so you can't manage things like priority:
# add to the dict:
named_rects['new_rect'] = Rectangle(90,90,95,95)
# remove
del named_rects['new_rect']
# find = is there a known key?
if 'new_rect' in named_rects: print new_rect
# search:
right_of_5 = [rect for rect in named_rects.items() if rect.Left > 5]
There are cases where you need fancier things than plain old lists and dicts -- but always try it with the free stuff first :)
If you dynamically want to create variables and add them to class instances, use this
class MainClass:
def __setattr__(self, name, value):
self.__dict__[name] = value
def getRectangleNameGenerator(N = 10):
X = 0
while X <= N:
X += 1
yield "Rectangle" + str(X)
RectangleName = getRectangleNameGenerator()
ClassInstances = {next(RectangleName) : MainClass}
ClassInstances[next(RectangleName)] = MainClass
ClassInstances["Rectangle1"].Temp = 10
print ClassInstances["Rectangle1"].Temp
If the class is going to have only X and Y,
class MainClass:
X, Y = 0, 0
def getRectangleNameGenerator(N = 10):
X = 0
while X <= N:
X += 1
yield "Rectangle" + str(X)
RectangleName = getRectangleNameGenerator()
ClassInstances = {next(RectangleName) : MainClass}
ClassInstances[next(RectangleName)] = MainClass
ClassInstances["Rectangle1"].X = 11
print ClassInstances["Rectangle1"].X
If you really want to refer to your rectangle instances by name, I would suggest to keep a dictionary at class level. Something like this:
#! /usr/bin/python3
from threading import Lock
import random
class Rectangle:
instances = {}
lock = Lock ()
#classmethod
def forName (cls, name):
return cls.instances [name] if name in cls.instances else None
#classmethod
def push (cls, inst):
with cls.lock:
name = None
while not name or name in cls.instances:
name = ''.join (random.choice ('abcdefghij') for i in range (16) )
cls.instances [name] = inst
return name
def __init__ (self):
self.name = Rectangle.push (self)
names = [Rectangle ().name for i in range (5) ]
for name in names:
print (name, Rectangle.forName (name) )
I'm coding a simple test program in python as part of my greater program, but i would l like to pass a sub function name into the main function, so the main function can run the subfunction.
eg:
import datetime;
def cronjob(repeat, interval, task):
if (str(repeat) == 'inf'):
repeat = 99999999999999999999;
position = 0;
interval = datetime.timedelta(seconds=interval);
x = datetime.datetime.now()
while not (position >= repeat):
while (datetime.datetime.now() - x < interval):
pass;
x = datetime.datetime.now();
position += 1;
exec task;
def test():
print "hello";
cronjob(10, 0.1, 'test');
EDIT: Already fixed this, but since nothing is listed here, let me show you how to do it in case someone else needs it.
I fiddled with eval() and exec, and tried just eval(task). that didn't throw an error, so I tried print eval(task) and sure enough, it listed the function's memory address [that is, test()]. finally, I have used eval(task); to then call that function. below is the code fixing this:
import datetime;
def cronjob(repeat, interval, task):
if (str(repeat) == 'inf'):
repeat = 99999999999999999999;
position = 0;
interval = datetime.timedelta(seconds=interval);
x = datetime.datetime.now()
while not (position >= repeat):
while (datetime.datetime.now() - x < interval):
pass;
x = datetime.datetime.now();
position += 1;
eval(task);
def test():
print "hello";
cronjob(10, 0.1, 'test()');
Why not pass the function object itself to the scheduler ?
test is an object and can be used as an argument too!
def test():
print "test"
def exe(func):
func()
exe(test)
I believe since functions are objects, you can just pass one in to the "controlling" function by name, so you don't need the exec call (which is usually used for dynamic code execution).
e.g.
def foo(a_number, a_function):
print a_number
a_number += 1
a_function(a_number)
def bar(another_number):
print another_number
foo(5, bar)
should produce the output:
5
6
In case if you're absolutely sure you want to derive a function from a string, you may want to use a dict as a mapping from strings to functions like follows:
dispatcher = {'dothis': module1.do_this,
'dothat': module2.to_that}
def foo(fun):
fun(arg1, arg2)
def bar(action):
fun = dispatcher.get(action)
if fun:
foo(fun)
This will be much more secure (as action is likely to come from the outside) and provide better decoupling of internal code structure from the external API.