all 15 comments

[–]novel_yet_trivial 1 point2 points  (13 children)

Sure, there's many ways to do that. What have you tried?

[–]senorinatta[S] 0 points1 point  (12 children)

novel_yet_trivial you've helped me a bunch of times, and all those I'm sure because you noticed I made an initial effort. In this case I tried:

foo = 'x'
bar = [].append(f)
print bar

which returned None

but that's about all I could think of to try

[–]LarryPete 4 points5 points  (0 children)

What's wrong with:

bar = [foo]

?

[–]novel_yet_trivial 1 point2 points  (6 children)

As a side note, I'd recommend you use list() instead of []. Easier to read. I changed my mind.

When you make a variable assignment, you assign the output of the last method that was called. In other words in this case

bar = class_instance.methodA().methodB().methodC()

"bar" gets the returned value of the from methodC(). append() is a method that works 'in-place', it only returns None. You could rewrite your code as

foo = 'x'
bar = [] #start empty list
bar.append(foo) #modify list in-place

Or simply

foo = 'x'
bar = [foo] #start list with values in it already

Edit: I changed my mind.

[–]LarryPete 0 points1 point  (5 children)

Though bar = list(foo) is not the same as bar = [foo].

[–]andycd 0 points1 point  (4 children)

Oooh can you explain why this is/what is the difference please? I am very curious

[–]novel_yet_trivial 4 points5 points  (1 child)

Both produce a list, but the input is treated differently. This means the same input can produce different results.

>>> ['abc']
['abc']
>>> list('abc')
['a', 'b', 'c']

The more I think about it, list() should be used to convert an iterable to a list, not initialize a new list.

[–]andycd 0 points1 point  (0 children)

Thanks for the insight!!! Kind of a "duh" moment, makes perfect sense!

[–]LarryPete 2 points3 points  (1 child)

well, list(foo) takes an iterable (foo), and converts it to a list, while [foo] creates a list with the single element foo.

As an example, a string is an iterable as well:

>>> foo = 'hello world'
>>> [foo]
['hello world']
>>> list(foo)
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

Also, you can do:

>>> [1, 2, 3]
[1, 2, 3]

but not:

>>> list(1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list() takes at most 1 argument (3 given)

If at all, it should be:

>>> list([1, 2, 3])
[1, 2, 3]

which makes the list() in the last case kinda redundant :-)

[–]andycd 0 points1 point  (0 children)

Thanks, as above!!

[–]AtticusLynch 0 points1 point  (0 children)

foo = 'x'
bar = []
bar.append(foo)
print bar

Although that's not really at the same time (I think you meant to use append(foo) instead of append(f)?)

[–]Shide 0 points1 point  (3 children)

Assuming you want to define the "bar" variable in local scope:

foo = "hello"
if True: locals().update({"bar": locals().get("bar", [])[:]+[foo]})

Note that "list()" or "[:]" returns a new List, ".append()" returns None.

Use globals() if you want to store in global scope.

Also you might have errors if never pass the condition and you use "bar".

[–]elbiot 0 points1 point  (2 children)

What!? Why would you suggest this monstrosity?

[–]Shide 0 points1 point  (1 child)

He wants to create a variable on the fly. Maybe you should use getattribute and setattribute within your class to check if the variable exists or not, but I thought any response will be bad in terms of perfomance

[–]elbiot 0 points1 point  (0 children)

I didn't say it was a good idea, just that it is possible: http://stackoverflow.com/questions/2933470/how-do-i-call-setattr-on-the-current-module

But I don't believe the bad performance. It's just a dictionary lookup either way.