1

I am quite new to python, so pardon me for basic question. I tried google for past few days but could not make it in my program.

Can anyone show me a good example how can I use method from One class to another in python and what is significance of __init__ while defining class.

I am using python2.7

Thanks in anticipation.

asked Jul 30, 2015 at 5:49
3

3 Answers 3

4

To use a method defined in one class inside of another class, you have several options:

  1. Create an instance of B from within one of A's methods, then call B's method:

    class A:
     def methodInA():
     b = B()
     b.methodInB()
    
  2. If appropriate, use the concept of inheritance (one of the defining concepts of object-oriented design) to create a subclass of the original class whose method(s) you wish to use:

    class B(A):
     ...
    

__init__() is a class initializer. Whenever you instantiate an object you are invoking __init__() whether or not it is explicitly defined. It's main purpose is to initialize class data members:

class C:
 def __init__(self, name):
 self.name = name
 def printName(self):
 print self.name
c = C("George")
c.printName() # outputs George

With __init__() defined, in particular with the additional argument name in this example, you are able to differentiate between would-be generically constructed instances by allowing for different initial states from instance to instance.

Sign up to request clarification or add additional context in comments.

4 Comments

I want to do like two classes are in different python files.
I believe the import keyword is what you are looking for. Once you've imported the file containing one of the classes into the other file the above still applies.
so it will be like "from fileA.py import classA; " Correct??
from fileA import ClassA is fine
2

There are 2 issues here:

First: Using method of class A in class B, both classes in different files

class A:
 def methodOfA(self):
 print "method Of A"

let the above class be in file a.py Now the class B is supposed to be in b.py. Both a.py and b.py are assumed to be on the same level or in the same location. Then b.py would look like:

import a
class B:
 def methodOfB(self):
 print "Method of B"
 a.A().methodOfA()

You can also do this by inherting A in B

import a
class B(a.A):
 def methodOfB(self):
 print "Method of B"
 self.methodOfA()

there are several other ways to use A in B. I will leave it to you to explore.

Now to your second question. The use of __init__ in a class. __init__ is not a constructor, as popularly believed and explained above. It is, as the name suggests, an initialization function. It is called only after the object has already been constructed and it is implicitly passed the object instance as the first argument, as signified by self in its argument list.

The actual constructor in python is called __new__, which does not need a object to call it. This is actually a specialized Static method, which receives the class instance as the first argument. __new__ is exposed for overwriting only if the class inherits form the object base class of python

Whatever other arguments are passed while creating an object of a class, first go to __new__ and then are passed with the object instance to the __init__, if it accepts them.

answered Jul 30, 2015 at 7:03

1 Comment

Updated constructor to initializer. Thanks.
0

The init function is what is called a constructor function. When you create an instance of a class object = myClass(), init is the function that is automatically called. i.e.

That being said, to call a function from one class to another, you need to call an instance of the second class inside the first one, or vice versa. for eg.

class One():
 def func(self):
 #does sometthing here
class Two():
 def __init__(self):
 self.anotherClass = One()
 #Now you can access the functions of the first class by using anotherClass followed by dot operator
 self.anotherClass.func()
#When you call the main class. This is the time the __init__ function is automatically called
mainClass = Two()

Another way to access from another class is the use of oop concept called Inheritance.

class One():
 def __init__(self):
 print('Class One Called')
 def func(self):
 print('func1 Called')
class Two(One):
 def __init__(self):
 One.__init__(self,) #This basically creates One's instance
 print('Main Called')
c= Two()
c.func()

The output for this is:

Class One Called
Main Called
func1 Called

answered Jul 30, 2015 at 6:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.