colours = [turtle.color("red"),turtle.color("blue"),turtle.color("yellow"),turtle.color("green")]
fred = colours[0],turtle.forward(100),turtle.left(90),colours[1],turtle.forward(100),turtle.left(90),colours[2],turtle.forward(100),turtle.left(90),colours[3],turtle.forward(100),turtle.left(90)
Attemping to make a square with 4 different colours from a list, type(colours[0]) returns class Nonetype. How can I access the colours from my list?
Your code:
colours = [turtle.color("red")]
Will run the function turtle.color("red"), and store the return value in the list.
This is exactly the same as doing:
colours = [None]
If you call colours[0] you get the return value, not the function. Python has no idea if the None ended up there through a function call, or if you just assigned it manually.
You only posted 2 lines of code, so I don't quite know what the context is here, but you may want to do something like:
colours = [lambda: turtle.color("red"), lambda: turtle.color("blue")]
What this does, is store a lamba (or 'anonymous function') in your list. This function is not executed. You will now get:
>>> colours[0]
<function <lambda> at 0x80089e710>
And you can execute this as many times as you want by appending parenthesis, like so: colours[0]()
This technique is known as 'currying' by the way.
Related
I'm working on getting a better grasp of Python 3 fundamentals, specifically objects and modifying them in the context of a list (for now).
I created a simple class called MyThing() that just has a number, letter, and instance method for incrementing the number. My goal with this program was to create a list of 3 "MyThings", and manipulate the list in various ways. To start, I iterated through the list (obj_list_1) and incremented each number using each object's instance method. Easy enough.
What I'm trying to figure out how to do is perform the same operation in one line using the map function and lambda expressions (obj_list_2).
#!/usr/bin/env py
import copy
class MyThing:
def __init__(self, letter='A', number=0):
self.number = number
self.letter = letter
def __repr__(self) -> str:
return("(letter={}, number={})".format(self.letter, self.number))
def incr_number(self, incr=0):
self.number += incr
# Test program to try different ways of manipulating lists
def main():
obj1 = MyThing('A', 1)
obj2 = MyThing('B', 2)
obj3 = MyThing('C', 3)
obj_list_1 = [obj1, obj2, obj3]
obj_list_2 = copy.deepcopy(obj_list_1)
# Show the original list
print("Original List: {}".format(obj_list_1))
# output: [(letter=A, number=1), (letter=B, number=2), (letter=C, number=3)]
# Standard iterating over a list and incrementing each object's number.
for obj in obj_list_1:
obj.incr_number(1)
print("For loop over List, adding one to each number:\n{}".format(obj_list_1))
# output: [(letter=A, number=2), (letter=B, number=3), (letter=C, number=4)]
# Try using map function with lambda
obj_list_2 = list(map(lambda x: x.incr_number(1), obj_list_2))
print("Using maps with incr_number instance method:\n{}".format(obj_list_2))
# actual output: [None, None, None] <--- If I don't re-assign obj_list_2...it shows the proper sequence
# expected output: [(letter=A, number=2), (letter=B, number=3), (letter=C, number=4)]
if __name__ == "__main__":
main()
What I can't figure out is how to get map() to return the correct type, a list of "MyThing"s.
I understand that between Python 2 and Python 3, map changed to return an iterable instead of a list, so I made sure to cast the output. What I get is a list of 'None' objects.
What I noticed, though, is that if I don't re-assign obj_list_2, and instead just call list(map(lambda x: x.incr_number(1), obj_list_2)), then print obj_list_2 in the next line, the numbers get updated as I expect.
However, if I don't cast the map iterable and just do map(lambda x: x.incr_number(1), obj_list_2), the following print statement shows the list as having not been updated. I read in some documentation that the map function is lazy and doesn't operate until it's use by something...so this makes sense.
Is there a way that I can get the output of list(map(lambda x: x.incr_number(1), obj_list_2)) to actually return my list of objects?
Are there any other cool one-liner solutions for updating a list of objects with their instance methods that I'm not thinking of?
TL;DR: Just use the for-loop. There's no advantage to using a map in this case.
Firstly:
You're getting a list of Nones because the mapped function returns None. That is, MyThing.incr_number() doesn't return anything, so it returns None implicitly.
Fewer lines is not necessarily better. Two simple lines are often easier to read than one complex line.
Notice that you're not creating a new list in the for-loop, you're only modifying the elements of the existing list.
list(map(lambda)) is longer and harder to read than a list comprehension:
[x.incr_number(1) for x in obj_list_2]
vs
list(map(lambda x: x.incr_number(1), obj_list_2))
Now, take a look at Is it Pythonic to use list comprehensions for just side effects? The top answer says no, it creates a list that never gets used. So there's your answer: just use the for-loop instead.
This is because, your incr_number doesn't return anything. Change it to:
def incr_number(self, incr=0):
self.number += incr
return self
The loop is clearly better, but here's another way anyway. Your incr_number doesn't return anything, or rather returns the default None. Which is a false value, so if you simply append or x, then you do get the modified value instead of the None
Change
list(map(lambda x: x.incr_number(1), obj_list_2))
to this:
list(map(lambda x: x.incr_number(1) or x, obj_list_2))
I'm trying to dynamically add function calls to fill in array columns. I will be accessing the array millions of times so it needs to be quick.
I'm thinking to add the call of a function into a dictionary by using a string variable
numpy_array[row,column] = dict[key[index containing function call]]
The full scope of the code I'm working with is too large to post here is an equivalent simplistic example I've tried.
def hello(input):
return input
dict1 = {}
#another function returns the name and ID values
name = 'hello'
ID = 0
dict1["hi"] = globals()[name](ID)
print (dict1)
but it literally activates the function when using
globals()[name](ID)
instead of copy pasting hello(0) as a variable into the dictionary.
I'm a bit out of my depth here.
What is the proper way to implement this?
Is there a more efficient way to do this than reading into a dictionary on every call of
numpy_array[row,column] = dict[key[index containing function call]]
as I will be accessing and updating it millions of times.
I don't know if the dictionary is called every time the array is written to or if the location of the column is already saved into cache.
Would appreciate the help.
edit
Ultimately what I'm trying to do is initialize some arrays, dictionaries, and values with a function
def initialize(*args):
create arrays and dictionaries
assign values to global and local variables, arrays, dictionaries
Each time the initialize() function is used it creates a new set of variables (names, values, ect) that direct to a different function with a different set of variables.
I have an numpy array which I want to store information from the function and associated values created from the initialize() function.
So in other words, in the above example hello(0), the name of the function, it's value, and some other things as set up within initialize()
What I'm trying to do is add the function with these settings to the numpy array as a new column before I run the main program.
So as another example. If I was setting up hello() (and hello() was a complex function) and when I used initialize() it might give me a value of 1 for hello(1).
Then if I use initialize again it might give me a value of 2 for hello(2).
If I used it one more time it might give the value 0 for the function goodbye(0).
So in this scenaro let's say I have an array
array[row,0] = stuff()
array[row,1] = things()
array[row,2] = more_stuff()
array[row,3] = more_things()
Now I want it to look like
array[row,0] = stuff()
array[row,1] = things()
array[row,2] = more_stuff()
array[row,3] = more_things()
array[row,4] = hello(1)
array[row,5] = hello(2)
array[row,6] = goodbye(0)
As a third, example.
def function1():
do something
def function2():
do something
def function3():
do something
numpy_array(size)
initialize():
do some stuff
then add function1(23) to the next column in numpy_array
initialize():
do some stuff
then add function2(5) to the next column in numpy_array
initialize():
do some stuff
then add function3(50) to the next column in numpy_array
So as you can see. I need to permanently append new columns to the array and feed the new columns with the function/value as directed by the initialize() function without manual intervention.
So fundamentally I need to figure out how to assign syntax to an array column based upon a string value without activating the syntax on assignment.
edit #2
I guess my explanations weren't clear enough.
Here is another way to look at it.
I'm trying to dynamically assign functions to an additional column in a numpy array based upon the output of a function.
The functions added to the array column will be used to fill the array millions of times with data.
The functions added to the array can be various different function with various different input values and the amount of functions added can vary.
I've tried assigning the functions to a dictionary using exec(), eval(), and globals() but when using these during assignment it just instantly activates the functions instead of assigning them.
numpy_array = np.array((1,5))
def some_function():
do some stuff
return ('other_function(15)')
#somehow add 'other_function(15)' to the array column.
numpy_array([1,6] = other_function(15)
The functions returned by some_function() may or may not exist each time the program is run so the functions added to the array are also dynamic.
I'm not sure this is what the OP is after, but here is a way to make an indirection of functions by name:
def make_fun_dict():
magic = 17
def foo(x):
return x + magic
def bar(x):
return 2 * x + 1
def hello(x):
return x**2
return {k: f for k, f in locals().items() if hasattr(f, '__name__')}
mydict = make_fun_dict()
>>> mydict
{'foo': <function __main__.make_fun_dict.<locals>.foo(x)>,
'bar': <function __main__.make_fun_dict.<locals>.bar(x)>,
'hello': <function __main__.make_fun_dict.<locals>.hello(x)>}
>>> mydict['foo'](0)
17
Example usage:
x = np.arange(5, dtype=int)
names = ['foo', 'bar', 'hello', 'foo', 'hello']
>>> np.array([mydict[name](v) for name, v in zip(names, x)])
array([17, 3, 4, 20, 16])
i have recently encountered some return values in python interactive mode(i never get create_line function's return values in script mode and in case testing the lines individual in interactive mode i get return values but the function does not create lines like i expected). I was wondering what these meant and why is the line not getting created. Here are the few I have encountered till now:
>>> canvas.create_line(134,100,134,400)
1
>>>
and i got these return values too
>>> def shape():
canvas.create_line(134,100,134,400)
canvas.create_line(134,400,234,400)
canvas.create_line(234,100,234,400)
canvas.create_line(234,100,134,100)
>>> shape()
>>> canvas.create_line(134,100,134,400)
5
>>> canvas.create_line(134,400,234,400)
6
>>> canvas.create_line(234,100,234,400)
7
>>> canvas.create_line(234,100,134,100)
8
>>>
in this code, when i called shape function then it did not create the lines. Calling these lines individually gave me these return values, the odd this was how the values started from 5 and then went till 8 consecutively. Can someone please tell if this is a bug or do these numbers have a meaning?
When you say canvas.create_line() it returns an Id of the canvas object created, that later can be used to modify or get properties and so on.
Here is something about create_arc() as I could not find anything official about create_line():
The constructor returns the object ID of the new arc object on canvas.
From your example, seems like only create_line() and create_image() returns integer id, the rest gives an object id.
So for example:
obj = canvas.create_line(134,100,134,400)
canvas.itemconfig(obj, width=10)
Canvas methods take tagorId, so you can always just use an option tag='line1' and then call on it later, like:
obj = canvas.create_line(134,100,134,400,tag='line1')
canvas.itemconfig('line1', width=10)
in this code, when I called shape function then it did not create the lines.
Are you sure the canvas coordinates are correct? It starts from 0,0 of the canvas, try changing the coordinates and configure what works for your canvas.
I have three similar functions in tld_list.py. I am working out of mainBase.py file.
I am trying to create a variable string which will call the appropriate function by looping through the list of all functions. My code reads from a list of function names, iterates through the list and running the function on each iteration. Each function returns 10 pieces of information from separate websites
I have tried 2 variations annotated as Option A and Option B below
# This is mainBase.py
import tld_list # I use this in conjunction with Option A
from tld_list import * # I use this with Option B
functionList = ["functionA", "functionB", "functionC"]
tldIterator = 0
while tldIterator < len(functionList):
# This will determine which function is called first
# In the first case, the function is functionA
currentFunction = str(functionList[tldIterator])
Option A
currentFunction = "tld_list." + currentFunction
websiteName = currentFunction(x, y)
print(websiteName[1]
print(websiteName[2]
...
print(websiteName[10]
Option B
websiteName = currentFunction(x, y)
print(websiteName[1]
print(websiteName[2]
...
print(websiteName[10]
Even though it is not seen, I continue to loop through the iteration by ending each loop with tldIterator += 1
Both options fail for the same reason stating TypeError: 'str' object is not callable
I am wondering what I am doing wrong, or if it is even possible to call a function in a loop with a variable
You have the function names but what you really want are the function objects bound to those names in tld_list. Since function names are attributes of the module, getattr does the job. Also, it seems like list iteration rather than keeping track of your own tldIterator index would suffice.
import tld_list
function_names = ["functionA", "functionB", "functionC"]
functions = [getattr(tld_list, name) for name in function_names]
for fctn in functions:
website_name = fctn(x,y)
You can create a dictionary to provide a name to function conversion:
def funcA(...): pass
def funcB(...): pass
def funcC(...): pass
func_find = {"Huey": funcA, "Dewey": funcB, "Louie": FuncC}
Then you can call them, e.g.
result = func_find["Huey"](...)
You should avoid this type of code. Try using if's, or references instead. But you can try:
websiteName = exec('{}(x, y)'.format(currentFunction))
I first wrote a function that took 18 arguments and turned them into 6 different lists. Here is the code:
def list_maker(val1,val2,val3,val4,val5,val6,val7,val8,val9,por1,por2,por3,hth1,hth2,hth3,sat1,sat2,sat3):
#Make the voip list
list1 = [val1,val2,val3]
list2 = [val4,val5,val6]
list3 = [val7,val8,val9]
#Make the variable list
list_por = [por1,por2,por3]
list_hth = [hth1,hth2,hth3]
list_sat = [sat1,sat2,sat3]
return list1,list2,list3,list_por,list_hth,list_sat
That part worked just fine (I'll make it look better once it actually works).
Now, my idea was to use that function as input to this other function right below to create plots:
def graph_maker(listx1,listx2,listx3,list1,list2,list3):
#plot the saturation graph
por_plot = plt.plot(listx1,list1)
por_plot.ylabel('VOIP')
por_plot.xlabel('Porosity')
por_plot.show()
#plot the heigth graph
hth_plot = plt.plot(listx2,list2)
hth_plot.ylabel('VOIP')
hth_plot.xlabel('Height')
hth_plot.show()
#plot the saturation graph
sat_plot = plt.plot(listx3,list3)
sat_plot.ylabel('VOIP')
sat_plot.xlabel('Saturation')
sat_plot.show()
So I ran the code with the two following lines:
list_maker(voip1,voip2,voip3,voip4,voip5,voip6,voip7,voip8,voip9,0.3,0.2,0.15,100,150,200,0.8,0.6,0.5)
graph_maker(list_maker)
And the error I'm getting is:
graph_maker() missing 5 required positional arguments: 'listx2',
'listx3', 'list1', 'list2', and 'list3'
What I understand from it, it looks as if list_maker() is actually returning only one list, and obviously the graph_maker function needs 6 arguments. Any ideas?
Thanks for the help!
Marco, when you pass list_maker into graph_maker, you're not actually passing the result of the function (the lists you want) as input to graph_maker, you're actually passing the function into it.
But it's not just a matter of doing this:
result = list_maker(voip1,voip2,voip3,voip4,voip5,voip6,voip7,voip8,voip9,0.3,0.2,0.15,100,150,200,0.8,0.6,0.5)
graph_maker(result)
Because the function list_maker returns a tuple with all the lists, you need to expand them this way:
result = list_maker(voip1,voip2,voip3,voip4,voip5,voip6,voip7,voip8,voip9,0.3,0.2,0.15,100,150,200,0.8,0.6,0.5)
graph_maker(*result)
The asterisk will expand the tuple into the 5 arguments that the function requires, does this make sense?
You're missing part of the inner function call in the outer function call:
graph_maker(list_maker)
graph_maker(*list_maker(vars))
Or assign your initial function call to a variable and use * to unpack the values (credit to #zondo)
x=list_maker(voip1,voip2,voip3,voip4,voip5,voip6,voip7,voip8,voip9,0.3,0.2,0.15,100,150,200,0.8,0.6,0.5)
graph_maker(*x)