Home When defining class method, why there an equals sign in argument e.g. func(foo=3)
Reply: 1

When defining class method, why there an equals sign in argument e.g. func(foo=3)

Jason
1#
Jason Published in 2017-12-06 22:05:25Z

This question already has an answer here:

  • What does ** (double star/asterisk) and * (star/asterisk) do for parameters? 15 answers

I'm learning python, trying to figure out how does this code actually work:

def func(**args):
    class BindArgs(object):
       foo = args['foo']
       print 'foo is ', foo
       def __init__(self,args):
           print "hello i am here"
    return BindArgs(args) #return an instance of the class

f = func(foo=2)

Output:

foo is 2 hello i am here

But it's very confused that in the argument of a function func(foo=2) that takes equation mark in it. Could you please explain how the flow works?

Ahmad
2#
Ahmad Reply to 2017-12-06 22:20:49Z

Here is an abstract: You call the function func and pass a dictionary as the argument to it. In the func function, you define a class named BindArgs, and then in the return statement, you first make an object (instance) from the BindArgs class and then return that instance. Notice that the statement foo = args['foo'] get the key value of 'foo' from the args dictionary (that is 2 in your sample code). the init also will be run as the constructor of the class when you are creating an object.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298609 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO