Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
SimpleRSSreader
-
Hello, all.
Already some programs , I tried to add a little only , the function .
If you are interested , please try .
For those of the original author , thank you .SimpleRSSReader
https://gist.github.com/beer2011/fcc4aaeec2a0f489b8fa -
@beer2011 , thanks for sharing. Nice to read through the code for ideas. One thing I spotted, that appears it's a potential problem.
feeds=[]class FeedListController(object): def __init__(self, feeds=[]): self.feeds = feeds self.table = None
From what I have been reading a few days ago, dynamic objects should not be in the param list as there are only ever evaluated once. Can produce some strange bugs. Maybe I am wrong, maybe only a problem for functions.
From what I remember they say better to do feeds = None -
The problem and a potential solution:
def default_args_broken(my_list=[]): my_list.append(len(my_list)) print(my_list) def default_args_working(my_list=None): my_list = my_list or [] my_list.append(len(my_list)) print(my_list) default_args_broken() # [0] default_args_broken() # [0, 1] default_args_broken() # [0, 1, 2] default_args_working() # [0] default_args_working() # [0] default_args_working() # [0]
-
my_list or []
can have unintended side effects too because an empty list is evaluated as False. Consider this:def append_something(my_list=None): my_list = my_list or [] my_list.append(len(my_list)) foo = [] append_something(foo) print foo # => [] -- huh? bar = [0] append_something(bar) print bar # => [0, 1]
(Granted, the example is a bit contrived.)
-
And that is why you should write the
None
check using ternaryif
-else
instead ofor
:my_list = [] if my_list is None else my_list
-
This post is deleted! -
The reply to everyone who, thank you.
In fact, I, programming is amateur.
(For even English, but it is the same.sorry. ^^;; )
And to understand what has been taught, it is likely to take some time.
Again, thank you. -
@beer2011 , understand. I am also learning. Still brought up a good discussion. I hadn't been aware of this proble. A tricky one.