Pages

Tuesday, September 3, 2013

In python, objects are referred as references

Python uses shallow copy for object copying. If you see this code, The value b.a.val is modified externally.
class A(object):
    def __init__(self):
        self.val = 20
    
class B(object):
    def __init__(self, a):
        self.a = a
    
if __name__ == "__main__":
    a = A()

    b = B(a)
    print b.a.val # 20
    a.val = 40
    print b.a.val # 40
You need to use copy.deepcopy to avoid this pitfall.
from copy import *

class A(object):
    def __init__(self):
        self.val = 20
    
class B(object):
    def __init__(self, a):
        self.a = deepcopy(a)
        
    
if __name__ == "__main__":
    a = A()

    b = B(a)
    
    print b.a.val
    a.val = 40
    print b.a.val

13 comments:

  1. hello friend Ive seen on stackoverflow that you had a problem in python with: Q : How can I change the attribute of everything in a whole directory structure?

    Have you solved it?can you help me too?:(
    I wont take much of your time
    hope you have some free time to help me
    this is my email: jqkker22ro@yahoo.com
    Wish you a great day !

    ReplyDelete
  2. An iOS app development class offers guided and interactive learning. It supports hands-on project work. This ios app development class boosts coding confidence. It is effective.

    ReplyDelete
  3. “Nice share! Our Java course focuses on real-world coding skills with project-based learning.” java coaching online

    ReplyDelete
  4. “Helpful post! Our UI/UX program teaches practical design skills for user-friendly digital products.”ui design course online

    ReplyDelete
  5. iOS app development training teaches real-time mobile application creation techniques. It focuses on practical implementation. This ios app development training improves development expertise. Students work on live projects. Application lifecycle concepts are included. Performance optimization is covered. Assignments are provided. It builds professional confidence.

    ReplyDelete
  6. Tableau developer training delivers expert-level knowledge in building interactive dashboards and analytical reports for organizations. It explains advanced calculations and data blending clearly. This tableau developer training enhances enterprise visualization skills. Learners design real-time analytics solutions. Practical assignments are included. Performance optimization techniques are covered. It prepares professional BI developers.

    ReplyDelete
  7. Mule software training focuses on the core features of the MuleSoft platform, such as integration, data processing, and API management. Get hands-on practice and gain the expertise to handle complex integrations confidently.mule software training

    ReplyDelete
  8. ServiceNow training helps you master incident management, change management, and automated workflows. By the end of the training, you’ll be able to configure and manage ServiceNow environments efficiently.servicenow training

    ReplyDelete
  9. Great read! dell boomi course
    is a perfect way to learn modern cloud integration and data connectivity techniques.

    ReplyDelete
  10. Great article! Our dell boomi course
    offers hands-on training in cloud integration, API management, and workflow automation for real-world applications.

    ReplyDelete
  11. Sales Force Admin Course
    Is sales force admin course easy to learn? I saw this sales force admin course.
    → Yes, it’s beginner-friendly.
    → Good to know, thanks!

    ReplyDelete
  12. .Great article! Very helpful for readers looking for clear insights. For those interested in skill development, check out best training institute in Chennai offering quality courses. Helpful information about best guest posting sites.

    ReplyDelete