[Tutor] Fwd: Re: would someone please explain this concept to me
nathan tech
nathan-tech at hotmail.com
Wed Jun 5 06:47:09 EDT 2019
Thought I addressed this to the list... Aparrently my mail client hates me.
-------- Forwarded Message --------
Subject: Re: [Tutor] would someone please explain this concept to me
Date: Wed, 5 Jun 2019 02:37:49 +0100
From: nathan tech <nathan-tech at hotmail.com><mailto:nathan-tech at hotmail.com>
To: Steven D'Aprano <steve at pearwood.info><mailto:steve at pearwood.info>
Hiya,
Thanks, first, for being able to understand what I was asking.
I admit I read over my own email and went, uh... I think I need to rewrite that.
So, I see what you are saying there, and it makes sense.
I want to actually do the following though:
d={} # a dict of some kind
feed1=somefeed
feed2=another feed of some kind
d["feed 1's url"]=feed1
d["feed 2's url"]=feed2
The way I do this is there are certain properties, if you were, that also need to be included, I.e, each entry should be its own dict containing feed, last check, check regularity, ETC.
That is why I was going to use g.blank-feed as the template, assign that to d["feed 1's link"] then just update the feed part.
Is there a way to do this?
Thanks
Nathan
On 05/06/2019 02:08, Steven D'Aprano wrote:
On Tue, Jun 04, 2019 at 11:37:23PM +0000, nathan tech wrote:
globals.py:
feeds={}
blank_feed={}
blank_feed["checked"]=1
blank_feed["feed"]=0
That is more easily, and better, written as:
feeds = {}
blank_feed = {"checked": 1, "feed": 0}
main file:
import globals as g
# some code that loads a feed into the variable knm
Do you mean something like this? If so, you should say so.
knm = "some feed"
g.feeds[link]=g.blank_feed;
What's "link" here? And there is no need for the semi-colon.
g.feeds[link]["feed"]=knm
Right... what the above line does is *precisely* the same as
g.blank_feed["feed"] = knm
Follow the program logic. I'm inserting 1970s BASIC style line numbers
to make it easier to discuss the code, remember that you can't actually
do that in Python.
10: g.feeds[link] = g.blank_feed
20: g.feeds[link]["feed"] = knm
Line 10 sets g.feeds[link] to the dict "blank_feed". *Not* a copy: you
now have two ways of referring to the same dict:
"g.blank_feed" and "g.feeds[link]"
both refer to the one dict, just as "Nathan" and "Mr Tech" are two ways
of referring to the same person (you).
So line 20 does this:
- look for the name "g", which gives the "globals.py" module;
- inside that module, look for the name "feeds", which gives
the "feeds" dict;
- look inside that dict for the key "link" (whatever value
that currently holds), which by line 10 has been set to
the same dict "blank_feed".
- inside the blank_feed dict, set key "feed" to "knm".
#in the below code, the variable link has a different value:
# load a feed into the variable r
Something like this?
r = "a different feed"
g.feeds[link]=g.blank_feed
Now you have *three* ways of naming the same dict:
"g.blank_feed", "g.feeds[link]", "g.feeds[different_link]"
but they all point to the same dict.
g.feeds[link]["feed"]=r
Now at this point, python would set the first loaded feed to the same
thing as the second loaded feed. It also set g.blank_feed to the second
feed, as well.
No, there is only one feed in total. You just keep updating the same
feed under different names.
I replaced the last three lines with this:
# load a feed into the variable r
g.feeds[link]=g.blank_feed;
g.feeds[link]["feed"]=r
And it works.
I don't see any difference between the replacement code and the original
code. The code you show does exactly the same thing.
but why does it work?
Why does that semi unlink all the variables?
Semi-colon?
It doesn't. You must have made other changes as well, semi-colons don't
have any runtime effect. They are *purely* syntax to tell the parser to
seperate multiple statements on one line.
More information about the Tutor
mailing list