python - making a function as an else inside an __init__ -


how function inside if/else inside __init__ :

class foo(object):     def __init__(self, q, **keywords):         if == "":             print "no empty strings"         else:             def on_g(self, response):                 if response.error:                     print "check internet settings"                 else:                     self.bar()             http_client.fetch("http://www.google.com/", self.on_g) 

because program dont read on_g() if put empty string!

if use on_g() outside in parallel __init__() need declared variable, example:

class foo(object):     def __init__(self, q, **keywords):         if == "":             print "no empty strings"         else:             self.on_g()    def on_g(self):        print 'hello there' 

will return hello there

self (the instance you're creating through __init__ ) doesn't have on_g method.

functions class-es need defined @ class level (as shown on second chunk of code). evaluated when class first... erm... "looked-up"? "evaluated"?

that's why second piece of code works. how come can call self.on_g within __init__ when actual definition of on_g method seems come later in code? it's odd behavior (at first glance) interpreter, right? well... that's because when run self.on_g(), whole foo class has been evaluated , on_g has been added class (not instance!: class)

class foo(object):    def __init__(self, q, **keywords):        [ . . . ]        else:            self.on_g()      # can use self.on_g() eventhough defined... _                             #                                                 |                             #                                                 |    def on_g(self):          # <------------ later  ---------------------------|        print 'hello there' 

whereas if define method within __init__, interpreter yell @ you:

class test(object):     def __init__(self):         def test(self):             print "hello"         self.test()  = test() 

throws:

traceback (most recent call last):   file "./test.py", line 10, in <module>     = test()   file "./test.py", line 8, in __init__     self.test() attributeerror: 'test' object has no attribute 'test' 

even if think oh, maybe class doesn't have test method because it's still within __init__, , have once initialization completed... meeeck... wrong:

class test(object):     def __init__(self):         def test(self):             print "hello"  = test() a.test() 

same attributeerror.

if still want add on_g class @ runtime (very bad idea, imho) can interpreter's job doing this:

class test(object):     def __init__(self):         def test(self):             print "hello"         self.__class__.test = test         self.test()  = test() a.test() 

... correctly prints:

hello hello 

now, 2 straightforward things can think of are:

  1. you move def on_g(self) class level (as showed in second code snippet)
  2. you call http_client.fetch on_g function local __init__'s scope (being picky language: on_g function, not method, since not bound object anymore).

    def __init__(self, q, **keywords):     if == "":         print "no empty strings"     else:         def on_g(response):             if response.error:                 print "check internet settings"             else:                 self.bar()         http_client.fetch("http://www.google.com/", on_g) 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -