Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 846199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:34:35+00:00 2026-05-15T06:34:35+00:00

I have a class with attributes which have a reference to another attribute of

  • 0

I have a class with attributes which have a reference to another attribute of this class. See class Device, value1 and value2 holding a reference to interface:

class Interface(object):
    def __init__(self):
        self.port=None

class Value(object):
    def __init__(self, interface, name):
        self.interface=interface
        self.name=name

    def get(self):
        return "Getting Value \"%s\" with interface \"%s\""%(self.name, self.interface.port)

class Device(object):
    interface=Interface()
    value1=Value(interface, name="value1")
    value2=Value(interface, name="value2")

    def __init__(self, port):
        self.interface.port=port

if __name__=="__main__":
    d1=Device("Foo")
    print d1.value1.get() # >>> Getting Value "value1" with interface "Foo"
    d2=Device("Bar")
    print d2.value1.get() # >>> Getting Value "value1" with interface "Bar"
    print d1.value1.get() # >>> Getting Value "value1" with interface "Bar"

The last print is wrong, cause d1 should have the interface “Foo”. I know whats going wrong: The line interface=Interface() line is executed, when the class definition is parsed (once). So every Device class has the same instance of interface.

I could change the Device class to:

class Device(object):
    interface=Interface()
    value1=Value(interface, name="value1")
    value2=Value(interface, name="value2")

    def __init__(self, port):
        self.interface=Interface()
        self.interface.port=port

So this is also not working: The values still have the reference to the original interface instance and the self.interface is just another instance…

The output now is:

>>> Getting Value "value1" with interface "None"
>>> Getting Value "value1" with interface "None"
>>> Getting Value "value1" with interface "None"

So how could I solve this the pythonic way? I could setup a function in the Device class to look for attributes with type Value and reassign them the new interface. Isn’t this a common problem with a typical solution for it?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:34:36+00:00Added an answer on May 15, 2026 at 6:34 am

    interface is a class attribute. So when you do

    d2=Device("Bar")
    

    you are changing the port of the interface for all objects of class Device.


    If you want to have these attributes per object instance, you have to put them into the __init__ method:

    class Device(object):
        def __init__(self, port):
            self.interface=Interface()
            self.value1=Value(self.interface, name="value1")
            self.value2=Value(self.interface, name="value2")
            self.interface.port=port
    

    You should only use class attributes if you really want to share a value with all the objects.

    Other than that, I don’t know what you try to accomplish here. Why defining it as class attribute in the first place?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, it's a really bad idea to use… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer If you are not dead set on using a listbox,… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer killproc will terminate programs in the process list which match… May 16, 2026 at 9:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a class which has a private attribute which is a reference to
I have two methods, one called straight after another, which both throw the exact
I have a custom attribute: public class MenuItemAttribute : Attribute { } and a
In a WCF service, I have two classes with the [DataContract] attribute. One of
I have a very simple method: Class Team(models.Model): def sides(self): return SideNames.objects.filter(team=self) SideNames is
I have set up a ruleset in my configuration file which has two validators,
I have a question about the way in which the output is displayed. <?php
I have a Drupal view which should output a video player using flash. I
I have an ASP.NET 3.5 SP1 Webforms Application. I use the MVP pattern (supervising
Let's say you want to diagram a Map class (the mathematical kind, not the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.