sparkle Plenty wrote:
> On Wed, Apr 24, 2013 at 4:21 PM, Prasad, Ramit <ramit.pra...@jpmorgan.com> 
> wrote:
[snip]
> >
> > Here is my function as it stands now:
> >
> >   def conCatNumbers(numOne, numTwo, numThree):
> >     global instanceGroup, lengthCounter, instanceCounter
> >     print("Pre Pack:  ", numOne, " ", numTwo, " ", numThree)
> >     packValues = struct.pack("HBB", numOne, numTwo, numThree)
> >     print("Post Pack: ", packValues)
> >     instanceValues = (packValues, )
> >     print(instanceValues)
> >     instanceGroup = (instanceGroup, )
> >     instanceGroup = instanceGroup + instanceValues
> >     lengthCounter += 4
> >     instanceCounter += 1
> >     return (instanceGroup, lengthCounter, instanceCounter)
> >
[snip]
> >>> blah = []
> >>> def foo():
> ...     instanceValues = '\x04\xd2\x01\x00'
> ...     blah.append( instanceValues ) # module level object can be mutated 
> without global keyword,
>                                       # just not reassigned (name binding).
> ...     print blah
> ...
[snip]
> ~Ramit
> 
> Thank you so much!  I knew the Global statement was a cop-out, but I was 
> frustrated with the endless
> Unbounded Local Variable errors, and getting a little wild-eyed and desperate 
> as my project deadline
> approaches.  I will implement the changes you suggested, and then retest.

Note that the above example is still using a global variable (actually a module 
level). 
You can eliminate it by just passing in the list as well.

def pack_without_globals(lst, numOne, numTwo, numThree):
    '''Pack values into hex bytes and append to lst.
    '''
    packValues = struct.pack("HBB", numOne, numTwo, numThree)
    lst.append( packValues )
    # No need to return lst because it is modified in place and the calling 
function should 
    # have access to the object anyway. 

> 
>  I have also searched the Tutor archives quite a lot, and noted a number of 
> websites that I will
> peruse for another online course (I have taken the one at Python.org) so I 
> can review class and
> function best practices.  It is great to have someone to ask when I get stuck.

That is the purpose of this list. :)


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to