Re: A question about unicode() function



On 31 Dec 2006 05:20:10 -0800, JTree <eastera@xxxxxxxxx> wrote:
def funUrlFetch(url):
lambda url:urllib.urlopen(url).read()

This function only creates a lambda function (that is not used or
assigned anywhere), nothing more, nothing less. Thus, it returns None
(sort of "void") no matter what is its argument. Probably you meant
something like

def funUrlFetch(url):
return urllib.urlopen(url).read()

or

funUrlFetch = lambda url:urllib.urlopen(url).read()


objUrl = raw_input('Enter the Url:')
content = funUrlFetch(objUrl)

content gets assigned None. Try putting "print content" before the unicode line.

content = unicode(content,"gbk")

This, equivalent to unicode(None, "gbk"), leads to

TypeError: coercing to Unicode: need string or buffer, NoneType found

None's are not strings nor buffers, so unicode() complains.

See ya,

--
Felipe.
.



Relevant Pages

  • Re: Default parameters
    ... The default parameter value bindings are made a def time. ... The comparable def code to your lambda would be ... >One might argue that this could could benefit from resolving ...
    (comp.lang.python)
  • Re: whats wrong with "lambda x : print x/60,x%60"
    ... >> One perhaps needs to be a little more careful with instance variables, ... > def callback(): ... But this seems to defeat the main argument for removing lambda ...
    (comp.lang.python)
  • Re: Anonymous methods, blocks etc. (Cont. default block params)
    ... > room was that either using defor lambda() was generally felt to be ... Using def(), however, means that "def" is ... non-closure is at all more efficient that's agood thing. ... > much magic punctuation in Ruby as it stands now. ...
    (comp.lang.ruby)
  • Re: How to create functors?
    ... variables into the functions the lambda is wrapping. ... def MyFunction: ... CreateTask(lambda: SomeOtherFunction(localVariable)) # CreateTask ... >> It looks like in your version of Python "print" isn't a function. ...
    (comp.lang.python)
  • Re: Why anonymity? [was Re: map/filter/reduce/lambda opinions and background unscientific mini-s
    ... > Because it obfuscates your code for no benefit. ... expression above is within a gnat's whisker of the def equivalent, ... I don't see why lambda puts people off. ... Traceback: ...
    (comp.lang.python)