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

  • Home
  • SEARCH
  • 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 8657669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:34:43+00:00 2026-06-12T15:34:43+00:00

Although i’m familiar with other programming languages, I’m struggling to understand my code in

  • 0

Although i’m familiar with other programming languages, I’m struggling to understand my code in python and why my class properties aren’t reset when i reassign the class… eg. I have a method in a notify class

def send(self):
    for service in self.hosts:
      # func = getattr(self, service)
      for host in self.hosts[service]:
        try:
          if service == 'xbmc':
            self.xbmc(host)
          elif service == 'growl':
            self.growl(host)
          elif service == 'prowl':
            self.prowl(host)
        except:
          print "Cound't connect " + host.find('ip').text + " For " + service

As you can see, this will iterate through each service (so keep in mind I have 3 xbmc’s)

so take xbmc for an example, the method is

def xbmc(self, host):
  x = Xbmc(host)
  x.notify(self.title, self.message)

under other languages, if i would assign x again, it would be a completely new fresh object, but this doesnt seem to be the case with python…

I will place snippets off my xbmc class to explain the issue.

class Xbmc:
  hosts = []
  def __init__(self, host = None):
    self.setHosts(host)
    self.server = Hosts().getHostsByService('xbmcserver')

  def setHosts(self, host = None):
     ...alot of elif...
     elif type(host).__name__ == 'Element': # this code is the one that is in effect from my notify class
       self.hosts.append(host)
     ...more elif...
     print self.hosts # for debugging

now that the code is there, when i initiate my notify to send. the output shows:

[<Element 'host' at 0x7fdda19d0d10>]
XBMC 192.168.1.10 Failed To Connect
[<Element 'host' at 0x7fdda19d0d10>, <Element 'host' at 0x7fdda1955090>]
XBMC 192.168.1.10 Failed To Connect
XBMC 192.168.1.20 Failed To Connect
[<Element 'host' at 0x7fdda19d0d10>, <Element 'host' at 0x7fdda1955090>, <Element 'host' at 0x7fdda19553d0>]
XBMC 192.168.1.10 Failed To Connect
XBMC 192.168.1.20 Failed To Connect
XBMC 192.168.1.21 Failed To Connect

its clear that when i append the host, that its never actually a new instance but always appends to what was already there… I assumed x = Class() would initiate a New Instance? if i do

self.hosts = [host]

then obviously i dont get the undesired effect, but it would be good too know why my initial approch doesnt work or how to overcome it. Do we need a reset method? or is there actually a way to create a new instance off a class? or is there something wrong with my code? 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-06-12T15:34:44+00:00Added an answer on June 12, 2026 at 3:34 pm

    Problem

    The problem is caused, because the class property hosts is mutable (list), so appending to it will influence every instance.

    Solution

    The solution is to change the class property into instance property:

    class Xbmc:
        # <--- (no more "hosts" here)
        def __init__(self, host = None):
            self.hosts = []  # <--- we are initializing "hosts" property here now
            self.setHosts(host)
            self.server = Hosts().getHostsByService('xbmcserver')
        # and so on...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Although this code is from an algorithm text, I have a bit of a
Although I am using Doxygen for some years, there are lots of other methods
Although this is django related, it's really just a general, programming efficiency question. I
Although I am not new to Python, this is my first attempt at using
Although I couldn't find a reference to this easily in google, I'm familiar with
although I have been using python a long time very easily in a Linux
Although ServerSocket class is designed to generically listen for incoming connections, Can I just
Although I do understand the serious implications of playing with this function (or at
Although I'm programming in C++, this is more of a general question about design.
Although other browsers seems fine, IE7 is not friendly with my 10 lines javascript.

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.