[Tutor] Fw: Misc question about scoping

ALAN GAULD alan.gauld at btinternet.com
Fri Jun 4 01:00:13 CEST 2010


Fowarding to the list.
Remember to use Reply ALL when replying.

 
Alan G.


"Tino Dai" <oberoc at gmail.com> wrote
>>>
>>>
>>>>>>>   Is there a way to express this:
>>>>>>>>   isThumbnail = False
>>>>>>>>   if size == "thumbnail":
>>>>>>>>       isThumbnail = True
>>>>
>>>>>>>>    like this:
>>>>>>>>    [ isThumbnail = True if size == "thumbnail" isThumbnail = False ]
>>>>
>>>
>>>Bob showed one way, you could also do:
>>>
>>>>>>isThumbnail = True if size == "thumbnail" else False
>>>
>>>
>>>
>>>>>>>    and the scoping extending to one level above without resorting to the
>>>>>>>>global keyword?
>>>>
>>>
>>>Not sure what this has to do with scoping or the use of global?
>>>>>>global is only needed inside a function if you are modifying a
>>>>>>value defined outside the function.
>>>
>>>>>>If the assignment is inside a function and the definition of
>>>>>>isThumbnail is outside then you need to use global.
>>>
>>>>>>HTH,
>>>
>>>
>> 
>>Hi Bob, Alan, and Joel,
>>
>>     I may have used scoping in an incorrect way. To clear things up, let me try to explain my general issue:
>>
>>     I have code that is unpythonic in many places. It works, but it's ugly. One of those unpythonic places is I'm initializing some variable such as a list,dict, or whatever outside of if/while/for in block to use later. So, I'm cleaning those places up. 
>>
>>      For example, my present code looks like:
>>      L = []     # Needed or L doesn't exist outside of for loop below
>>      for o in a:
>>         if o.someAttribute > someConstant:
>>            L.append(o.someOtherAttribute)
>>>>
>>      .... later in code ....
>>      <some use of L>
>>
>>      Would like my code to resemble (don't know if this code works):
>>      L = [o.someOtherAttribute if o.someAttribute > someConstant else pass for o in a]
>>>>
>>      .... later in code ....
>>      <some use of L>
>>
>> Does that make more sense?
>>
>>TIA,
>>Tino     
>>On Thu, Jun 3, 2010 at 1:10 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>>
>Here is some actual code that I working on right now:
>
>First iteration:
>>answerDict={}
>for key in qas[0].fk_question.q_WidgetChoice.all():
>    answerDict[str(key.widgetChoice)]=0
>
>Second iteration:
>answerDict=dict([(str(key.widgetChoice),0) for key in qas[0].fk_question.q_widgetChoice.all()])
>
>Third iteration:
>answerDict=dict(map(lambda x:  \
>      (str(x.widgetChoice),0),qas[0].fk_question.q_WidgetChoice.all()))
>
>The third iteration is what I'm looking for.
>
>-Tino
>On Thu, Jun 3, 2010 at 1:39 PM, Tino Dai <oberoc at gmail.com> wrote:
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100603/48b2a11f/attachment-0001.html>


More information about the Tutor mailing list