How to pass one object to another class in python

"What I do not understand is b = Bar(a). What does it do?"

b = Bar(a) does two things. First, it creates an object of class Bar (with any class variables and methods attached). Then, it runs __init__ with the first argument (self) pointing to the object that was just created, and with a as the second argument (arg). While running __init__, as one of the commands in that method, it sets self.arg to point to the object pointed to by arg (i.e. to the object pointed to by the variable a). Finally, the variable b is set to refer to the object that was created.

It may help to think this way: a variable in Python is really just a pointer that points to an object. You can have more than one variable pointing to the same object. In this case, a and b.arg both point to the same object.

I found this sort of thing confusing too, at first. I had seen the advice to think of variables as separate concepts from the object they point to and ignored it as unnecessarily complicating things, but I had to go back to accepting that way of thinking in order to make sense of things. People do often use the variable as a name to refer to the object it points to; you just have to know when to take this literally or not.

"Wont that mean Bar inherit from a?"

No. If a is a class, then class Bar(a) would mean that Bar inherits from a. But in b = Bar(a), a is an object being passed as an argument to __init__.

"What is Bar.Foo1 = Foo?"

Sorry -- I don't see that in the example code you gave.

"What is the meaning of b.arg.variable?"

b is an object (i mean, b refers to an object) and b.arg is one of the attributes of that object. Methods and variables are different types of attributes. In this case, b.arg is a variable pointing to an object. The object referred to by b.arg has attribute variable, which is a variable.

b.arg refers to the same object that a refers to, therefore b.arg.variable is the same variable as a.variable. It not only points to the same object, but actually is the same variable. The object it points to is the string "something".

@Brenbarn: I think that's what quirius meant by "Wont that mean Bar inherit from a?".

In this post we’ll see how to pass object of the class as parameter in Python.

Passing object as parameter

In the example there are two classes Person and MyClass, object of class Person is passed as parameter to the method of class MyClass.

class MyClass():
  def my_method(self, obj):
    print('In my_method method of MyClass')
    print("Name:", obj.name)
    print("Age:", obj.age)

In MyClass there is one method my_method which takes one more argument apart from

self

.

 
from MyClass import MyClass
class Person:
  def __init__(self, name, age):
    print('init called')
    self.name = name
    self.age = age

  def display(self):
    print('in display')
    print("Name-", self.name)
    print("Age-", self.age)
    # object of class MyClass
    obj = MyClass()
    # passing person object to
    # method of MyClass (self = person here)
    obj.my_method(self)

person = Person('John', 40)
person.display()

In class Person, MyClass is also used so that is imported.

from MyClass import MyClass

In method display() object of MyClass is created.

obj = MyClass()

Then the my_method() method of class MyClass is called and object of Person class is passed as parameter.

 # passing person object to
 # method of MyClass (self = person here)
 obj.my_method(self)

On executing this Python program you get output as following.

init called
in display
Name- John
Age- 40
In my_method method of MyClass
Name: John
Age: 40

Recommendations for learning (Udemy Courses)

  1. Complete Python Bootcamp Course
  2. The Complete Python Course-Learn Python by Doing
  3. Python and Django Full Stack Web Developer Bootcamp
  4. Python for Data Science and Machine Learning
  5. Java Programming Masterclass Course

That's all for this topic Passing Object of The Class as Parameter in Python. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Class And Object in Python
  2. Constructor in Python - __init__() function
  3. Multiple Inheritance in Python
  4. Method Overloading in Python
  5. Abstract Class in Python

You may also like-

  1. Accessing Characters in Python String
  2. Python Conditional Statement - if, elif, else Statements
  3. Python Program to Display Armstrong Numbers
  4. JVM Run-Time Data Areas - Java Memory Allocation
  5. Linear Search (Sequential Search) Java Program
  6. Passing Arguments to getBean() Method in Spring
  7. Connection Pooling With Apache DBCP Spring Example
  8. Word Count MapReduce Program in Hadoop

How do you pass an object from one class to another in Python?

If we have two different classes and one of these defined another class on calling the constructor. Then, the method and attributes of another class can be accessed by first class objects ( i.e; objects within objects ). Here in the below example we learn to access object (its methods and attributes) within an object.

How do you pass an object to another class?

The simplest and most common ways are:.
as a constructor parameter,.
as a parameter of a setter method; e.g. setFoo(Foo foo) to set the "foo" attribute, or..
as an "add" method in the object being passed is going to be added to a collection; e.g. addFoo(Foo foo) ..

Can we create object of one class in another class in Python?

A class defined in another class is known as an inner class or nested class. If an object is created using child class means inner class then the object can also be used by parent class or root class. A parent class can have one or more inner classes but generally inner classes are avoided.

Can you pass an object to a function in Python?

Python's language reference for assignment statements states that if the target is an object's attribute that supports assignment, then the object will be asked to perform the assignment on that attribute. If you pass the object as an argument to a function, then its attributes can be modified in place.