How to pass one object to another class in python
"What I do not understand is b = Bar(a). What does it do?" Show
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, 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 "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?"
@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 parameterIn 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)
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
You may also like-
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.
|