Python Forum
Class Instances overriding class members and attributes.
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Instances overriding class members and attributes.
#8
Thank you for your detailed example. My long-version of the code looks similar to this but I broke it down piece by piece to work on playing and exploring different aspects of Python. My goal is to learn to work with classes and to learn to get comfortable with passing mutable data to those classes and understanding exactly what is going on before refactoring the code to actually do things more efficiently. I want to understand what I'm doing and why things work the way they do.

Yesterday Blueflash offered me a major key (or keys):

(1) That when I create an instance I also create a dictionary at the same time in the class.
(2) When I call the add_person function it updates an existing dictionary with the key-values, and creates a new instance with each new one. # But esp. that my instructions in add_function and my lack of understanding of how python is interpreting the data is at work here.
(3) That a class should contain by design information about a single item.

I realized I was dealing with 2 separate problems. First I was dealing with the problem that data I sent through the function was sticky my llists would print out as 0 3 3 3 (this is when PID had a counter to it), and when I put an id on the object the first object was different and the 3 objects were different. And depending on how I did the code it sometimes printed out 3 3 3 3, the last instance created was the one witht he values. At the same time I was fighting with the data being created in the add_person but not being able to copy it to a new list anywhere because of 0 3 3 3 pattern; and because it would erase the data as soon as I created it. My goal was that before it erased the data to send it to a new global list that was shared between all instances or to pass it somewhere else within the same instance. And now that I understand more of what's going on under the hood I might be able to do that.

But what Blueflash told me yesterday gave me a hint to explore more and understand more.

So here is what I discovered.

I create an instance:
x = Person(data) <-- where this data is sort of template I want to modify.
then I used it to add_person.
a = x.add_person("Person1")
b = x.add_person("Person2"
c = x.add_person("Person3")

originally each value was given the same value but when I made the list outside of the add_person functions I needed to create list = [a, b, c] for it to work.

I tried to append it to a global list and even a class outside the list but had the same problem, (1) 0 3 3 3 (the data was sticking) or 3 3 3 3 (the last instance was being added three times to the list) and (2) that the list erased itself each time a new element was created.


the argument "data" was the cause for 0 3 3 3. If I changed data to, data2, or data3 the classes stopped trying to modify and override the original "data" and instead create new data.

This information allowed me to research in better terms what was happening. I remember saying it was as if Python remembers the values of the previous data, and it turns out python does.

It's the same problem I had when I first began working with importing in python and python functions. In that situation I couldn't understand how python import works and scope; in the function there was a similar problem. Once I learned how Python thinks and processes information that made sense. Classes are similar.

http://python.net/~goodger/projects/pyco...-variables

http://effbot.org/zone/default-values.htm

https://www.programiz.com/python-programming/closure

http://docs.python-guide.org/en/latest/writing/gotchas/

I was writing code and expecting it to do one thing and it did another:

(1) understanding how mutable objects vs. immutable objects react differently when passed as arguments.
(2) understanding how python binds variable data
(3) making sure I understand instance members vs. class members/attributes
(4) understanding closure in python

Even though I didn't have a nested function behaviour appears to be similar.

This allowed me to successfully achieve the goal I wanted and I was with few adjustments to this practice code, able to add the items one by one to the new list. With that knowledge I hope to attempt to write it more efficiently and will rewrite it a few times until it is more efficient.

I just wanted to share that as an update as I enjoyed the insight.

@Windspar

Thank you for your code example. I've saved it for reference for when I get to my next goal. Another reference I am also using is the family tree generator available on github. My goal is to learn from the code and then create something simple using similar concepts. But I had to break the problem down so I know the code I've shared here is not the ideal way to do things; but it's how I learn. Trying it one way, learning why it doesn't work well or efficiently, refactoring and refactoring with new experience gained each time.

My next goal is to build another class Lineages, and to explore what I have been calling callbacks and async coding but don't necessarily know if that's an accurate term. It's what I'm using to say when one function calls another function and another and they pass information back and forth to achieve the same thing. That's the ultimate outcome of what I'm attempting to code but have to work through it piece by piece to understand what I'm doing.

I'm not sure if what I've concluded or what I understand is accurate or not; so if I've made any mistakes in my understanding or if there are other insights on how python thinks and best practices or formulas (recipes) for achieving what I want, a pattern of coding I'll take it! :D
Reply


Messages In This Thread
RE: Class Instances overriding class members and attributes. - by Leaf - Nov-29-2017, 06:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class and methods ebn852_pan 3 216 Yesterday, 06:35 AM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 200 May-13-2024, 05:57 AM
Last Post: Larz60+
  Class and methods Saida2024 2 209 May-13-2024, 04:04 AM
Last Post: deanhystad
  How does this code create a class? Pedroski55 6 590 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 323 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 362 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 458 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 626 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 806 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 847 Feb-04-2024, 09:35 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020