Discussion:
[Tutor] generate a list/dict with a dynamic name..
bruce
2015-09-27 16:38:05 UTC
Permalink
Hi.

I can do a basic
a=[]
to generate a simple list..

i can do a a="aa"+bb"

how can i do a
a=[]

where a would have the value of "aabb"

in other words, generate a list/dict with a dynamically generated name

IRC replies have been "don't do it".. or it's bad.. but no one has
said you can do it this way..

just curious..

thanks
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Chris Warrick
2015-09-27 16:45:20 UTC
Permalink
Post by bruce
Hi.
I can do a basic
a=[]
to generate a simple list..
i can do a a="aa"+bb"
how can i do a
a=[]
where a would have the value of "aabb"
in other words, generate a list/dict with a dynamically generated name
IRC replies have been "don't do it".. or it's bad.. but no one has
said you can do it this way..
just curious..
thanks
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
And we really mean it.

There are two ways. One involves e**l, which means you are executing
arbitrary code, and if a contained some malicious code, it could break
your system. Using e**l is considered bad for this precise reason:
you don’t know if the input might lead to formatting your hard drive.
And you should not trust it to be good.

The other way involves modifying the g*****s() dict. It does not
always work though.

But for a REAL way to do it, just create a dict and use it — you can
have arbitrary variable names just fine:

things = {}
a = "aabb"
things[a] = []

PS. why are you creating a out of two strings? Why not just a = "aabb"?
--
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://ma
Mark Lawrence
2015-09-27 17:32:45 UTC
Permalink
Post by bruce
Hi.
I can do a basic
a=[]
to generate a simple list..
i can do a a="aa"+bb"
Really?
Post by bruce
Post by bruce
a="aa"+bb"
File "<stdin>", line 1
a="aa"+bb"
^
SyntaxError: EOL while scanning string literal

No. Moral of the story, never type something in directly, always cut
and paste using the interactive interpreter.
Post by bruce
how can i do a
a=[]
where a would have the value of "aabb"
in other words, generate a list/dict with a dynamically generated name
I've no idea what you mean in this context. `a` is the name you are
assigning some object to. `a = []` is a list, but having `a` be "aabb"
means `a` is a string. So do you want:-
Post by bruce
Post by bruce
a=2*'a'+2*'b'
a
'aabb'

Or
Post by bruce
Post by bruce
a=[2*'a'+2*'b']
a
['aabb']

Or what?
Post by bruce
IRC replies have been "don't do it".. or it's bad.. but no one has
said you can do it this way..
What replies? Please be extremely cautious on sites that are nowhere
near as precise as Python ones, for example stackoverflow or reddit.
There is some complete dross out there that gets upvoted, whereas on a
Python site it would be torn to pieces. To be fair, some answers are
good, please just be careful with what you read.
Post by bruce
just curious..
thanks
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-09-27 17:40:31 UTC
Permalink
Post by bruce
Hi.
I can do a basic
a=[]
to generate a simple list..
i can do a a="aa"+bb"
how can i do a
a=[]
where a would have the value of "aabb"
in other words, generate a list/dict with a dynamically generated name
You don't. That is a mistake. How would you use it? Suppose you could
wave a magic wand and create such a variable:

make_variable("aabb", 23) # this doesn't actually work
print aabb # prints 23

But what is the point of doing that? Since you already know the variable
name, just do it the good old fashioned way:

aabb = 23
print 23

"Ah," you say, "but what if I don't know what the name is until the
program actually runs?"

name = random.choice(['fred', 'barney', 'wilma', 'betty'])
make_variable(name, 23)
print ... # err, what the hell do I put here???


You can see the problem. `print name` doesn't work, since that will
print the name itself (say, 'fred'). Working with dynamically generated
names is just a nightmare. You **really** don't want to do there.

But... if you insist, and really want to shoot yourself in the foot...
Python does give you the magic wand to do it. (But please don't.)

In fact, two magic wands. Here's the first, using eval and exec:

py> name = "fred"
py> exec("%s = 23" % name)
py> eval(name)
23


But don't do that:

(1) It's confusing to anyone who has to understand your code.
(2) It's really hard to debug when something goes wrong.
(3) It's slow. About 10 times slower than regular Python code.
(4) It may be a security risk.

If the code you exec() or eval() ever comes from an untrusted users
(say, in a web application), you have just opened a serious back door
into your computer.

Here's another way:

py> name = "fred"
py> globals()[name] = 42
py> print globals()[name]
42

That's a bit better, but still, don't do it.

However, it does give us a clue how to do this safely. Instead of using
a global variable, we keep track of our "dynamic variables" in their own
custom namespace. (A namespace is just a fancy word for an isolated
collection of named things, i.e. variables.)

py> ns = {}
py> name = 'wilma'
py> ns[name] = 999
py> ns[name] += 1
py> print ns['wilma']
1000

By doing it this way, you can still dynamically generate your names, but
keep them isolated from the rest of the program. And you can easily
print out all your "variables" to see what's going on:

py> print ns
{'wilma': 1000}

It's just an ordinary dict.
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Danny Yoo
2015-09-27 18:31:40 UTC
Permalink
Post by bruce
Hi.
I can do a basic
a=[]
to generate a simple list..
i can do a a="aa"+bb"
how can i do a
a=[]
where a would have the value of "aabb"
in other words, generate a list/dict with a dynamically generated name
There is a conceptual split we make about things that are are "static"
(constant throughout time), and other things should be "dynamic"
(possibly changing throughout time).

In modern programming style, most people prefer that the program text
itself be static, but the *data* in the program may be dynamic. (This
preference toward static program text has not always been true, nor is
it universally true. Self-modifying programs do exist. A modern
example, for example, are Just-In-Time systems, where the program text
is reconstructed on-the-fly in response to the current execution of a
program.)

In general, most programming systems you'll see will strongly prefer
that the program text be static, because static systems are easier to
understand: their meaning doesn't change over time nor does it depend
on the circumstances of the outside world.

This being said, the preference for static program text doesn't mean
we can't handle dynamically generated names. We can place the
dynamism in the *data structures* that our programs handle, rather
than in the program text itself. A dictionary is a data structure
that can associate names to values, and that's a common tool we use to
do what you're asking.


For example:

######
my_dict = {}
my_dict['aabb'] = []
######

Here, the name 'aabb' is static: it's part of the program text.

But the names we add to a dictionary don't always have to be static.
A dictionary, in particular, allows those names to be dynamically
determined. If we are using Python 3, the following snippet will show
this in action:

#######
my_dict = {}
name = input('Please enter a name: ')
my_dict[name] = []
#######

and if we are using Python 2, do this instead:

#######
my_dict = {}
name = raw_input('Please enter a name: ')
my_dict[name] = []
#######
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Continue reading on narkive:
Search results for '[Tutor] generate a list/dict with a dynamic name..' (Questions and Answers)
6
replies
what is Bio Technology ?
started 2007-03-26 06:45:00 UTC
chemistry
Loading...