class - In the following code why does the last line of execution give an error? -


in following code why last line of execution give error ? shouldn't dot operator in x.bf() pass on instance 'x' function bf ( x.af() ) ?

class a:     = 6     def af (self):         return "hello-people"  class b:     b = 7     def bf (self):         return "bye-people"  >>> x = a() >>> b = b() >>> x.bf = b.bf >>> x.bf() traceback (most recent call last):   file "<stdin>", line 1, in <module> typeerror: bf() missing 1 required positional argument: 'self' 

x.bf = b.bf error, because b class, not instance of object.

you can't assign x.bf directly class. need assign x.bf instance 'b.bf' or instantiate class properly

ie. either change line to:

# instantiated class b , invoke bf via lazy loading (loading @ last possible minute) x.bf = b().bf  

or

# use existing instance of b , call bf x.bf = b.bf 

more information:

  1. a , b classes. don't until instantiate them.
  2. x , b object instances. x instance of a, , b instance of b
  3. whenever instantiate class, need conform it's constructor signature. in case, classes require no additional parameters besides self. however, self gets passed if class invoked via ();

    'x = a()' , 'b = b()' conform signature

    the error encountered python telling you called something, function or class without passing in required variable.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -