It seems their only value is storing attributes. Even then, those can't be changed or the changes get reflected across all instances!
If I have a class here:
# Vertex
class Vertex:
label = str()
ID = int()
outEdges = list()
inEdges = list()
make a new vertex object:
v = Vertex()
and add to v's outedges:
v.outEdges.append(1)
then Vertex.outEdges is no longer an empty list, but also contains 1.
So how should I use python classes? With the copy module? Not at all?
The attributes you are declaring are class-level, and are shared by all instances of the class. You need to use a proper initializer/constructor function if you want those values to be specific to an instance:
# Vertex
class Vertex:
def __init__(self):
self.label = str()
self.ID = int()
self.outEdges = list()
self.inEdges = list()
You can also create methods on classes, not just attributes.
class Vertex:
# def __init__(self): ...
def allEdges(self):
return self.outEdges + self.inEdges
You'll need an instantiation method if you want different instances of the class.
class Vertex:
def __init__(self):
self.label = str()
....
[..] the changes get reflected across all instances!
If you define attributes as you did they will behave as static attributes shared across all instances of given class.
If you want to get instances of given class which do not share attributes values you need to define it like that:
# Vertex
class Vertex:
def __init__(self):
self.label = str()
self.ID = int()
outEdges = list()
inEdges = list()
Related
I inherit features from one parent class (__Parent) to two different child classes for constructing a nested data structure. The init() method of ChildTwo initiallizes the parent class using super() thereby setting i_am_from_child to "Two". It then appends an instance of ChildOne to the inherited list var. It is this appending that behaves unexpected when using list.append(). The init() of ChildOne also initializes the parent class in the same way, setting its i_am_from_child to "One", but without appending its inherited list var.
Therfore the list var of the instance of ChildOne stored in ChildTwo's var[0] should have a length of 0 as it is empty. This behaviour is obtained when using numpy append. However pythons list.append() results in an instance of ChildOne beingg strored at that location.
import numpy as np
class __Parent:
var = []
i_am_from_child = None
def __init__(self, which:str=None) -> None:
self.i_am_from_child = which
print(f"Parent initiallized by Child{which}")
def how_long(self) -> int:
return len(self.var)
def from_child(self) -> str:
return self.i_am_from_child
class ChildOne(__Parent):
def __init__(self) -> None:
print("Initiallizing ChildOne")
super().__init__(which="One")
print(f"ChildOne: len(self.var) = {len(self.var)}")
class ChildTwo(__Parent):
def __init__(self) -> None:
print("Initiallizing ChildTwo")
super().__init__(which="Two")
# two options in question
self.var.append(ChildOne()) # this behaves unexpected
#self.var = np.append(self.var, ChildOne()) # this behaves as expected
#####################################################################
X = ChildTwo() # instance of ChildTwo
Y = X.var[0] # copy of ChildOne instance created by ChildTwo constructor
print(f"type(X) = {type(X)} and type(Y) = {type(Y)}")
print(f"X is from Child{X.from_child()} and Y=X.var[0] is from Child{Y.from_child()}")
# something weird with var happens when using diffent append methods
print()
print(f"ChildOne: self.function() = {Y.how_long()} should be 0")
print(f"ChildTwo: self.function() = {X.how_long()} should be 1")
print(f"Type of Y.var[0] is {type(Y.var[0])}")
Using print() I checked the correct sequence of method calles, additionally the types are correct. But Y.var[0] should be empty, i.e. [] and thus should have length zero. Commenting out the python append and uncommenting the numpy append statements in ChildTwo.init() produces the desired behaviour.
It's because you're declaring var as a class variable, which is shared between all instances of that class, rather than as an instance variable. When you type self.var, python first looks in the instance attribute dictionary, and if an attribute with the name var doesn't appear there, it looks in the instance's class dictionary, where it finds the shared var attribute. If you want each instance to treat them separately, you need to assign that variable in the __init__ method.
class Parent:
def __init__(self):
self.var = []
So I am trying to get my data structure set up for an automated generator I am writing for a roleplaying game and I am having trouble with some specific inheritance quirks. Here is an excerpt of the data structure.
class data():
def __init__(self):
self.races = Races()
class Races(data):
def __init__(self):
self.humans = Humans()
class Humans(Races):
def __init__(self):
self.Characteristics = {
'Brawn':2,
'Agility':2,
'Intellect':2,
'Cunning':2,
'Willpower':2,
'Presence':2
}
There is a lot more in the structure but this is just a bottom to top overview. I also know it is indented weirdly but that is strictly stack overflow.
Now I wish to have two behaviors from this object.
The ability to call any characteristic with
data.races.humans.Characteristic['brawn']
as the calling format.
And too also be able to iterate through subclasses with a generator like:
(subclass for subclass in data.races.__subclasses__())
obviously after I have instantiated the object.
Now I have tried changing the structure several times and I can get it so EITHER I can call it with dot notation, but it returns AttributeError: 'Races' object has no attribute '__subclasses__'
Or vice versa by completely separating it into a more traditional structure but then I cannot call in dot notation and this makes it very hard to keep everything organized and readable.
Can anyone suggest what I am doing wrong or a more Pythonic way to approach the problem?
Let's start in the middle. Presumably, a character of any race has the same attributes, just different values for those attributes.
class Race:
def __init__(self):
self.life = 100 # 100% healthy
class Humanoid(Race):
def __init__(self):
super().__init__()
self.legs = 2
class Insectoid(Race):
def __init__(self):
super().__init__()
self.legs = 8
class Human(Humanoid):
def __init__(self):
super().__init__()
self.brawn = 2
self.agility = 2
self.intellect = 2
self.cunning = 2,
self.willpower = 2
self.presence = 2
class Elf(Humanoid):
def __init__(self):
super.__init__()
self.brawn = 1
self.agility = 3
self.intellect = 3
self.cunning = 2
self.willpower = 3
self.presence = 1
Now, any particular character would be instantiated as the correct class:
some_elf_1 = Elf()
some_human_1 = Human()
some_human_2 = Human()
for character in [some_elf_1, some_human_1, some_human_2]:
print("Brawn: ", character.brawn)
In the preceding, it doesn't matter what the actual type of each character is; as long as you know that it is some subclass of Race (or an instance of Race itself), it will have a brawn attribute that you can access.
You data class doesn't really seem necessary without more detail.
So, While the answer given put me on the right track I realized what I needed and am just throwing in my lot for any poor souls.
Firstly - I realized what was wrong with my generator, I was calling on the initialized object instead of the class object. Objects do not have a subclasses attrib and I was mis-informed by most of the guides I read!
Secondly, I considered using a metaclass to get the iterating behavior I wanted from my objects can simply be achieved with a registry attribute that is a dict of all the initialized subclasses.
lass Races(data):
def __init__(self):
self.humans = Humans()
self.droids = Droids()
self.twileks = Twileks()
self.registry = {
'humans':self.humans,
'droids':self.droids,
'twileks':self.twileks
}
This allows me to iterate through certain values for different races after they have been initialized.
Thanks for all the great answers!
Say I have a very simple data type:
class SimpleObject:
def __init__(self, property):
self.property = property
def update_property(self, value):
self.property = value
And I a special kind of list to store the data type:
class SimpleList(collections.MutableSequence):
def update_useful_property_of_list(self, value):
self.useful_property_of_list = value
And I store them:
simple1 = SimpleObject(1)
simple2 = SimpleObject(2)
simple_list = SimpleList([simple1, simple2])
Is there any way for the SimpleList object to know when one of the properties of its members changes? For example, how can I get simple_list to execute self.update_useful_property_of_list() when something like this happens:
simple1.update_property(3)
As noted in the comments, you are looking for the Observer design pattern. Simplest, way to do it in your example:
class SimpleObject:
def __init__(self, property, propertyChangeObserver = None):
self.property = property
self.propertyChangeObserver = propertyChangeObserver
def registerPropertyChangeObserver(self, propertyChangeObserver):
self.propertyChangeObserver = propertyChangeObserver
def update_property(self, value):
self.property = value
if self.propertyChangeObserver:
self.propertyChangeObserver.simpleObjectPropertyChanged(self)
and:
class SimpleList(collections.MutableSequence):
def __init__(self, collection):
super(SimpleList, self).__init__(collection)
for e in collection:
e.registerPropertyChangeObserver(self)
def simpleObjectPropertyChanged(self, simpleObject):
pass # react to simpleObject.property being changed
Because you've called your property "property" it's hard to demonstrate low coupling here :) I've called the method simpleObjectPropertyChanged for clarity, but in fact, SimpleList doesn't have to know that it stores SimpleObject instances - it only needs to know that they are observable instances. In a similar manner, SimpleObject doesn't know about SimpleList - it only knows about some class that needs to observe its state (an observer - hence the name of the pattern).
I'm new to Python - and just trying to better understand the logic behind certain things.
Why would I write this way (default variables are in __init__):
class Dawg:
def __init__(self):
self.previousWord = ""
self.root = DawgNode()
self.uncheckedNodes = []
self.minimizedNodes = {}
def insert( self, word ):
#...
def finish( self ):
#...
Instead of this:
class Dawg:
previousWord = ""
root = DawgNode()
uncheckedNodes = []
minimizedNodes = {}
def insert( self, word ):
#...
def finish( self ):
#...
I mean - why do I need to use __init__ -> if I can just as easily add default variables to a class directly?
When you create variables in the Class, then they are Class variables (They are common to all the objects of the class), when you initialize the variables in __init__ with self.variable_name = value then they are created per instance and called instance variables.
For example,
class TestClass(object):
variable = 1
var_1, var_2 = TestClass(), TestClass()
print var_1.variable is var_2.variable
# True
print TestClass.variable is var_1.variable
# True
Since variable is a class variable, the is operator evaluates to True. But, in case of instance variables,
class TestClass(object):
def __init__(self, value):
self.variable = value
var_1, var_2 = TestClass(1), TestClass(2)
print var_1.variable is var_2.variable
# False
print TestClass.variable is var_1.variable
# AttributeError: type object 'TestClass' has no attribute 'variable'
And you cannot access an instance variable, with just the class name.
When you write this:
class Dawg:
previousWord = ""
root = DawgNode()
uncheckedNodes = []
minimizedNodes = {}
Those are not instance variables, they're class variables (meaning: the same variables with the same values are shared between all instances of the class.) On the other hand, this:
class Dawg:
def __init__(self):
self.previousWord = ""
self.root = DawgNode()
self.uncheckedNodes = []
self.minimizedNodes = {}
... Is declaring instance variables, meaning: the values are different for each instance of the class. As you see, each snippet means a completely different thing, and you have to pick the one that is appropriate for you. Hint: most of the time you're interested in instance variables, because class variables define a kind of shared global state for your objects, which is error prone.
Hi
I have created a List of Objects. Each object contains a Set. I want to update the set's contents for all the objects in the list. The code that i wrote to accomplish this is
class Player:
name = ""
cardsInHand = set()
hasBid = False
def __init__(self, name):
self.name = name
class CardDeck:
deck = []
def __init__(self):
for i in range(39) :
if i%10>0 and i%10<9 :
self.deck.append(i)
def dealCards(self,player):
cardIndex = 0
for tempPlayer in player:
for j in range(4): # since want to add four elements at a time
tempPlayer.cardsInHand.add(self.deck.pop(cardIndex))
cardIndex = cardIndex +1
in the main method I am calling the above classes with the following code
players = []
players.append(Player("Player0"))
players.append(Player("Player1"))
players.append(Player("Player2"))
players.append(Player("Player3"))
cards.dealCards(players)
The problem is that dealCards method adds the elements to all the sets of objects. Instead of 4 elements in each object's set, I endup with same 16 elements in each objects's set?
I am new to python, am i doing something wrong ?
You're creating class attributes.
class Player:
def __init__(self, name):
self.name = name
self.cardsInHand = set()
self.hasBid = False
You've defined cardsInHand (as well as name and hasBid) to be class variables instead of instance variables; by defining them in the class body, you're defining them to be variables shared by all instances. If you're familiar with Java, they are essentially like static variables. To make them instance variables, you need to declare them in the __init__ method, like so:
def __init__(self, name):
self.name = name
self.hasBid = False
self.cardsInHand = set()