Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Have two class instances affect one another
#1
Hey everybody,

newbe here. I'm trying out classes and I don't know how to get the desired results:

class name():
    def __init__(self,name):
        self.name = name
        self.m = 0
    def one(self):
        if self.name == 'Jack':
            self.m = self.m + 5
            print(self.m)
    
    def two(self):
        if self.m > 0:
            print(self.m)
        else:
            print("no")
S = name('Jack')
P = name('Tom')
I want to start with S.one() and then use P.two() to get 5 but I always get 'no' instead.
Any feedback is welcome and appriciated Big Grin
Quote
#2
So depending on exactly what you want, there's several ways to do it. The easiest would be to use a shared object to store the dependent data:
>>> class DataBag:
...   def __init__(self):
...     self.m = 0
...

>>> class Foo:
...   def __init__(self, name, data):
...     self.name = name
...     self.data = data
...   def one(self):
...     if self.name == "Jack":
...       self.data.m += 5
...   def two(self):
...     if self.data.m > 0:
...       print(self.data.m)
...     else:
...       print("no")
...
>>> bag = DataBag()
>>> s = Foo("Jack", bag)
>>> p = Foo("Tom", bag)
>>> s.one()
>>> p.two()
5
You could also use a class member instead of an instance variable, if you want to effect ALL instances of the class, not just these two:
>>> class Bar:
...   m = 0
...   def __init__(self, name):
...     self.name = name
...   def one(self):
...     if self.name == "Jack":
...       Bar.m += 5
...   def two(self):
...     if Bar.m > 0:
...       print(Bar.m)
...     else:
...       print("no")
...
>>> a = Bar("Jack")
>>> b = Bar("Fred")
>>> b.two()
no
>>> a.one()
>>> b.two()
5
I'd recommend the first, though, unless you have a good (and obvious) reason why all instances should have the same values.
Quote
#3
Hello,

I think it's right like result.

You set name of S to Jack and call one on this. So attribut m equal 5.

You set name of P with other value. So attribut m equal 0.

And you call two on P. It returns 'no' if m equal 0. This is the case.

Sorry, i have read the subject too fast.

I don't know if can it a solution, but you have the design pattern singleton for this.

You can found this on the web for python
Quote
#4
I actually do need all instances to have the same values so thanks a lot nilamo :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  TypeError: '>=' not supported between instances of 'str' and 'int' AsadZ 5 106 Yesterday, 02:02 PM
Last Post: ThomasL
  Instances as attributes sapra90 2 212 Jun-26-2019, 04:38 AM
Last Post: woooee
  '>' not supported between instances of 'str' and 'int' graham23s 2 398 May-11-2019, 07:09 PM
Last Post: micseydel
  Call a varaible from class in the parent class Clement_2000 1 309 May-09-2019, 11:14 PM
Last Post: michalmonday
  Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int sr12 8 747 Apr-11-2019, 08:19 PM
Last Post: sr12
  '<' not supported between instances of 'str' and 'int' jayaherkar 1 728 Apr-09-2019, 03:25 PM
Last Post: perfringo
  Get variable from class inside another class hcne 3 264 Mar-30-2019, 03:02 PM
Last Post: ichabod801
  TypeError: '<' not supported between instances of 'int' and 'builtin_function_or_meth yann2771 1 358 Mar-05-2019, 09:51 PM
Last Post: stranac
  Why Can't I call base class function for my derived class object? AmirAB 2 285 Feb-13-2019, 03:04 PM
Last Post: snippsat
  Calling a class inside other class dan789 9 515 Jan-06-2019, 02:00 PM
Last Post: dan789

Forum Jump:


Users browsing this thread: 1 Guest(s)