-8

Is there a beginner's tutorial on how to post a question on stackoverflow? I'm not completely stupid, but after a half-hour of editing and re-editing, I gave up. My post initially was as a commented, executable python script, but apparently that's unacceptable. I was continually told that parts looked like improper code. Removing "#" characters, highlighting and ctrl-K'ing the code, manually redoing all whitespace, etc., nothing worked. It can't accept a post as plain text? 'Put returns between paragraphs'? Huh? 'press the 10101010101 code button'? Where's that? (maybe a different site) Maybe I am a dummy. Thorough, step-by-step instructions would help me. Thanks.

==================================================================== I had to re-edit it, so I'm not sure if this is a correct Python module, but the original was similar ...

The posting seems to strip away "#" chars. I haven't attempted to format the code part ...

This may be elementary, but may help me understand namespaces.

A good explanation might step through what happens when the function definition is #executed, and then what happens later when the function object is executed.

Recursion may be complicating things.

The results aren't obvious to me; I would have expected:

locals_1 would contain var;

locals_2 would contain var and locals_1; and

locals_3 would contain var, locals_1, and locals_2

A function calls "locals" several times, and returns them ...

def func(): var = 'Hi!' locals_1 = locals() locals_2 = locals() locals_3 = locals() return locals_1, locals_2, locals_3

func is called ...

locals_1, locals_2, locals_3 = func()

display results ...

print 'locals_1:', locals_1 print 'locals_2:', locals_2 print 'locals_3:', locals_3

Here are the results:

locals_1: {'var': 'Hi!', 'locals_1': {...}, 'locals_2': {...}}

locals_2: {'var': 'Hi!', 'locals_1': {...}, 'locals_2': {...}}

locals_3: {'var': 'Hi!', 'locals_1': {...}, 'locals_2': {...}}

The pattern seems to be, with (n) calls to , all of the returned locals-dicts #are identical, and they all include the first (n-1) locals-dicts.

#Questions: #Why does locals_1 include itself? #Why does locals_1 include locals_2? Is locals_1 assigned when #the -object is #created, or executed?

And why is locals_3 not included anywhere?

#Does "{...}" indicate an 'endless recursion'? Sort of like those photos of mirrors #facing each other?


This may be elementary, but may help me understand namespaces. A good explanation might step through what happens when the function definition is executed, and then what happens later when the function object is executed. Recursion may be complicating things.

The results aren't obvious to me; I would have expected: locals_1 would contain var; locals_2 would contain var and locals_1; and locals_3 would contain var, locals_1, and locals_2

# A function calls "locals" several times, and returns them ...
def func():
    var = 'Hi!'
    locals_1 = locals()
    locals_2 = locals()
    locals_3 = locals()
    return locals_1, locals_2, locals_3

# func is called ...
locals_1, locals_2, locals_3 = func() 

# display results ...
print 'locals_1:', locals_1
print 'locals_2:', locals_2
print 'locals_3:', locals_3

Here are the results:

#locals_1: {'var': 'Hi!', 'locals_1': {...}, 'locals_2': {...}}
#locals_2: {'var': 'Hi!', 'locals_1': {...}, 'locals_2': {...}}
#locals_3: {'var': 'Hi!', 'locals_1': {...}, 'locals_2': {...}}

The pattern seems to be, with (n) calls to , all of the returned locals-dicts #are identical, and they all include the first (n-1) locals-dicts.

Questions:

  • Why does locals_1 include itself?
  • Why does locals_1 include locals_2? Is locals_1 assigned when #the -object is #created, or executed?
  • And why is locals_3 not included anywhere?

Does "{...}" indicate an 'endless recursion'? Sort of like those photos of mirrors #facing each other?

  • 10
    You were shown a "how to ask" page and required to indicate that you had read it before you were able to ask your question. I take it you didn't? – Servy Apr 7 '14 at 20:25
  • possible duplicate of FAQ for Stack Exchange sites – Servy Apr 7 '14 at 20:26
  • 6
    Where are you reading the quoted instructions? The 101010 button was changed to {} a while ago. – Bill the Lizard Apr 7 '14 at 20:30
  • Hmm. Did your post consist of only the code? That might be a problem. Could you upload it somewhere else, say on jsfiddle.com, so we can take a look at it? – Pekka Apr 7 '14 at 20:35
  • Put the question, exactly as you would have asked it, in your question here, so that we can have a look at it. – user102937 Apr 7 '14 at 20:54
  • @Servy Well, I think I searched a bit ... actually, looking at the FAQ, this site is a bit dense and bewildering for me ... that could be a good part of my problem. – tfj Apr 7 '14 at 20:54
  • @Bill the Lizard It was a web-search ... – tfj Apr 7 '14 at 20:55
  • @Pekka jsfiddle is dead? ... but it was a working python script, with comments, the code being the only uncommented part ... – tfj Apr 7 '14 at 21:01
  • @user sorry, jsfiddle.net – Pekka Apr 7 '14 at 21:11
  • Put the question, exactly as you would have asked it, in your question here, so that we can have a look at it. – user102937 Apr 7 '14 at 21:15
  • 1
    Dear god that is hard to read. – ThisSuitIsBlackNot Apr 7 '14 at 21:20
  • @RobertHarvey Yup, here it is! Python without code formatting = :( – Blue Ice Apr 7 '14 at 21:20
  • @Pekka et al - I've tried to enter something similar to the original - it starts with "This may ..." - and it looks a mess, but could you tell me, is simply cut-and-pasting a working python module not doable? – tfj Apr 7 '14 at 21:24
  • See my post below. – user102937 Apr 7 '14 at 21:27
  • 2
    @user3507800: The main rule: indent code 4 spaces. The { } button above the code window will do this for you automatically. I've pasted the edited question into your question above, so you can just click the edit link, and copy the text verbatim. – user102937 Apr 7 '14 at 22:20
5

Some suggestions for you. First, and most important, do not make the question part of your question come along for the ride as part of the code. Type the words and sentences into the question box as is and get them good (look at the preview below) before pasting in code. Perhaps you're thinking:

But it's all executable and my thoughts are there as comments!

That's not how things work here. We don't want code with some questions in it, we want a question with some code in it. (The system even detects all-code questions and tells you not to do that, as you may have noticed.) Usually, when you do include code it shouldn't all be as one giant wall of code. You can have some English sentences, then a few lines of code, then some more talking, a few more lines of code, and so on.

Do make a point of pasting in your code, not typing it. That way you won't introduce typos that obscure your actual question. After pasting in code, select it and use the { } button on the toolbar. This will indent it all 4 spaces so it is recognized as code.

this is code

Look at the preview. If it looks awful, make sure there's a blank line before the code and that it is sufficiently indented. Many legal characters in your code (like # at the start of a line) are interpreted as formatting if they are not indented.

Your two best friends are the preview area below the entry box, and the ? button just above it (at the right.) You will see plenty of examples and explanations there, though it won't explain the buttons that make life much easier than typing markdown by hand. You can also click edit on any post (by someone else) to see the raw markdown, then cancel editing. This lets you understand how some things were achieved.

2

This may be elementary, but may help me understand namespaces. A good explanation might step through what happens when the function definition is executed, and then what happens later when the function object is executed. Recursion may be complicating things.

The results aren't obvious to me; I would have expected: locals_1 would contain var; locals_2 would contain var and locals_1; and locals_3 would contain var, locals_1, and locals_2

# A function calls "locals" several times, and returns them ...
def func():
    var = 'Hi!'
    locals_1 = locals()
    locals_2 = locals()
    locals_3 = locals()
    return locals_1, locals_2, locals_3

# func is called ...
locals_1, locals_2, locals_3 = func() 

# display results ...
print 'locals_1:', locals_1
print 'locals_2:', locals_2
print 'locals_3:', locals_3

Here are the results:

#locals_1: {'var': 'Hi!', 'locals_1': {...}, 'locals_2': {...}}
#locals_2: {'var': 'Hi!', 'locals_1': {...}, 'locals_2': {...}}
#locals_3: {'var': 'Hi!', 'locals_1': {...}, 'locals_2': {...}}

The pattern seems to be, with (n) calls to , all of the returned locals-dicts are identical, and they all include the first (n-1) locals-dicts.

Questions:

  • Why does locals_1 include itself?
  • Why does locals_1 include locals_2? Is locals_1 assigned when the -object is created, or executed?
  • And why is locals_3 not included anywhere?

Does "{...}" indicate an 'endless recursion'? Sort of like those photos of mirrors facing each other?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .