Forum Archive

SimpleRSSreader

beer2011

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

Phuket2

@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

ccc

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]
omz

@ccc

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.)

dgelessus

And that is why you should write the None check using ternary if-else instead of or:

my_list = [] if my_list is None else my_list
techteej

hmm, seem to recognize this ;)

beer2011

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.

Phuket2

@beer2011 , understand. I am also learning. Still brought up a good discussion. I hadn't been aware of this proble. A tricky one.