Look at this simple snippet
from SwitchState import SwitchState
s1 = SwitchState()
s1.add(12345, True)
s2 = SwitchState()
print(s2.get_all())
Result is: [(12345, True)] !
I'm adding the item to s1 but got it in s2 too! What im doing wrong?
SwitchState.py
import struct
class SwitchState(object):
_states = []
def add(self, timestamp, state):
self._states.append((timestamp, state))
def get_all(self):
return self._states
They both have the same _states, since it's a class attribute.
class ...
def __init__(self):
self._states = []
Related
I am absolutely new to Wrapper Classes in Python. I was trying to implement it in a program of linked list where multiple linked list are in use. My code is:
def nodewrap(cls):
class Nodewrap:
def __init__(self):
self.head = None
self.tail = None
return Nodewrap
#nodewrap
class Node:
def __init__(self,data):
self.data = data
self.next = None
class Intersection:
def addnode(self,d):
newnode = Node(d)
if head == None:
head = tail = newnode
else:
tail.next = newnode
tail = newnode
obj1 = Intersection()
obj2 = Intersection()
obj3 = Intersection()
s1 = int(input("Enter size of 1st list : \n"))
for i in range(s1):
obj1.addnode(int(input("Enter the data : \n")))
s2 = int(input("Enter size of 1st list : \n"))
for i in range(s2):
obj2.addnode(int(input("Enter the data : \n")))
temp1 = obj1.head
for i in range(s1):
temp2 = obj2.head
for j in range(s2):
if temp1.data == temp2.data:
obj3.addnode(temp1.data)
break
temp2 = temp2.next
temp1 = temp1.next
print("Intersection is :")
temp = obj3.head
while temp!=None:
print(temp.data,end=" ")
temp = temp.next
I thought of using a wrapper class to wrap the class Node instead of using objects of the class Intersection only with data fields as head, tail. But it is giving me some sort of error with regards to init().
Please help.
I was trying to learn it from here:
https://www.geeksforgeeks.org/wrapper-class-in-python/
I think I understand what you want to do, but I think that you don't want to use a decorator, but you want to inherit from NodeWrap class
class Nodewrap:
head = None
tail = None
class Node(NodeWrap):
def __init__(self,data):
self.data = data
self.next = None
But I don't see any reason why to inherit this way. This should be enough, for a linked list. I have added is_first and is_last property
from __future__ import annotations
class Node:
prev_node = None
next_node = None
def __init__(self, data):
self.data = data
def add_node(self, node: Node) -> None:
if self.prev_node is not None:
raise ValueError('Previous node already defined.')
self.next_node = node
node.prev_node = self
#property
def is_first(self) -> bool:
return self.prev_node is None
#property
def is_last(self) -> bool:
return self.next_node is None
You can implement next, iter and create an Iterator class.
I don't recommend using next as the variable name.
from __future__ import annotations reference here. It's just for self reference annotation.
My code is provided in the end. I want to let the class general inherit all the variables of the constructor (_ init _) in the class LDA. It seems that when the method X_F in the class general calls the X_c, it just returns the objective rather than the array of np.random.rand(3,2) generated in the main. I tried to let the method X_F print the value of the variable m, but the printed result is a default value of 3, rather than 10.
class LDA:
def __init__(self, X_c, m = 3, K=1):
self.X_c =X_c
self.m =m
self.K = K
def Squared_L2_loss(self):
X_result = general(self).X_F()
return X_result
class general(LDA):
def X_F(self):
X = self.X_c[0]
print(self.m)
return X.T
if __name__ == '__main__':
X_c=np.random.rand(3,2)
X = LDA(X_c, m=10, K=30)
So I'm working with Linked Lists in python, and the UnitTest our professor gave us calls C = Course(), but asserts the values after. This is what they use to grade, but I can't find a way to call the class then wait for the variables, and apply them to the class without parameters so it doesn't crash. Thoughts? Am I missing something obvious?
Tried to only include the relevant code. If people need full code for clarification or just for kicks/giggles let me know.
import courselist
import course
def load_data(data):
with open(data) as f:
for line in f:
dline = line.strip().split(",")
C = course.Course(dline[0],dline[1],dline[2],dline[3])
course_list = courselist.CourseList()
course_list.insert(C)
return course_list
def main():
data = "data.txt"
full_list = load_data(data)
print(full_list.__str__())
main()
class Course:
def __init__(self, c_num, c_name, c_hour, c_grade):
self.c_num = c_num
self.c_name = c_name
self.c_hour = c_hour
self.c_grade = c_grade
self.next = None
class TestEmptyCourse(unittest.TestCase):
def test_course_creation(self):
# make sure that an empty course is correct
c = Course()
self.assertEqual(c.name(), "")
self.assertEqual(c.number(), 0)
self.assertEqual(c.credit_hr(), 0.0)
self.assertEqual(c.grade(), 0.0)
self.assertEqual(c.next, None)
I was missing something obvious... Hangs head in shame For those interested here's how to fix it. It's always when you ask for help that you get it just in time to look like an idiot haha. Thanks to those to checked it out. If someone has an alternate solution I'll be sure to upvote you
class Course:
def __init__(self, num=0, cname="", c_hr=0.0, cgrade=0.0, next=None):
self.num = num
self.cname = cname
self.c_hr = c_hr
self.cgrade = cgrade
self.next = None
def number(self):
return int(self.num)
def name(self):
return str(self.cname)
def credit_hr(self):
return self.c_hr
def grade(self):
return self.cgrade
def __str__(self):
return f"cs{self.num} {self.cname} Grade:{self.cgrade} Credit Hours: {self.c_hr}"
When I do not crate object for CP class, the operations are not captured. I am referring to the code below, Can somebody help me understand why we need obj creation in this case
from abc import ABC, abstractmethod
class P(ABC):
def __init__(self):
super().__init__()
self._pre_map = {}
self._pre_order = []
def set_pre(self, tag_value):
index = len(self._pre_map)
print(index)
self._pre_map[index] = tag_value
self._pre_order.append(index)
def execute(self):
pass
class CP(P):
def __init__(self):
super().__init__()
def execute(self):
self.prnt()
def prnt(self):
print (self._pre_map)
print (self._pre_order)
#Working
print("\n++++++++ working")
obj = CP()
obj.set_pre("test string added")
obj.execute()
#Not Working
print("\n+++++++ not working")
CP().set_pre("test string added")
CP().execute()
It produces,
++++++++working
0
{0: 'test string added'}
[0]
+++++++not working
0
{}
[]
When you call the class the second time with CP.execute(), you have created a completely new instance of the CP class. It is not going to have the text string you specified.
If you actually wanted it to print the values like the working one you can make the functions return self after each call in the P class. If you did that you could do something like this.
from abc import ABC, abstractmethod
class P(ABC):
def __init__(self):
super().__init__()
self._pre_map = {}
self._pre_order = []
def set_pre(self, tag_value):
index = len(self._pre_map)
print(index)
self._pre_map[index] = tag_value
self._pre_order.append(index)
##need to return self here
return self
def execute(self):
pass
class CP(P):
def __init__(self):
super().__init__()
def execute(self):
self.prnt()
def prnt(self):
print (self._pre_map)
print (self._pre_order)
#Working
print("\n++++++++ working")
obj = CP()
obj.set_pre("test string added")
obj.execute()
#Not Working
print("\n+++++++ not working: but now working after returning self in the P class")
CP().set_pre("test string added").execute()
++++++++ working
0
{0: 'test string added'}
[0]
+++++++ not working: but now working after returning self in the P class
0
{0: 'test string added'}
[0]
This would print the result you want.
The reason for the difference is the fact that in the first one, you are creating an instance, and using that instance the whole way through, whereas in the second one, you are using two different instances of your class.
The two different instances cannot share their attributes, so you are unable to recall what happened. If you really don't want to use a dedicated variable, change your P class to look like this:
class P(ABC):
...
def set_pre(self, tag_value):
index = len(self._pre_map)
print(index)
self._pre_map[index] = tag_value
self._pre_order.append(index)
return self
...
And use CP().set_pre("test string added").execute()
I want to use two instances of a base class from one derived class. I am not sure if the following code is technically correct or if there is a better way. Can the two instances mix up in any situation?
The base class A is intended to perform a common task: "changevar" which changes the variable "var" to "dv". The derived class B use this method but with two variables "var1", "var2" to change it to "dv1", and "dv2" respectively. class B then performs an "operation" over the changed variables "var1", and "var2" and return the result (in this case multiply).
test.py:
class A:
def __init__(self, var):
self.var = var
def changevar(self, dv):
self.var = self.var + dv
def getvar(self):
return self.var
class B(A):
def __init__(self, var1, var2):
self.var1 = var1
self.var2 = var2
A.__init__(self, var1)
self.inst1 = A(self.var)
A.__init__(self, var2)
self.inst2 = A(self.var)
def operation(self):
dv1 = 2
dv2 = -2
self.inst1.changevar(dv1)
self.inst2.changevar(dv2)
self.var1 = self.inst1.getvar()
self.var2 = self.inst2.getvar()
return self.var1, self.var2, self.var1 * self.var2
def main():
test = B(10, 10)
v1, v2, v3 = test.operation()
print (v1, v2, v3)
return
if __name__ == "__main__":
main()```
>>>python3 test.py
>>>12 8 96
In your code, you're not actually using the inheritance.
When you execute self.inst1 = A(self.var) you are creating a whole new instace of A, which is different from that which is the base for your B, and assigning it to a field.
Good news are: you don't need inheritance in your case. what you need (and de-facto do) is composition - having fields of type A in your type B.
Just remove A as a base from B at all, and use the inner fields of this type, the result will be the same:
class A:
def __init__(self, var):
self.var = var
def changevar(self, dv):
self.var = self.var + dv
def getvar(self):
return self.var
class B:
def __init__(self, var1, var2):
self.inst1 = A(var1)
self.inst2 = A(var2)
def operation(self):
dv1 = 2
dv2 = -2
self.inst1.changevar(dv1)
self.inst2.changevar(dv2)
var1 = self.inst1.getvar()
var2 = self.inst2.getvar()
return var1, var2, var1 * var2
def main():
test = B(10, 10)
v1, v2, v3 = test.operation()
print (v1, v2, v3)
return
if __name__ == "__main__":
main()
Inheritance should be used when you want to use instances of type B as if they were of type A (e.g., call A's methods on instances of type B).
Generally we say that B should inherit from A, ib B "is-a" A - e.g. a Dog is an Animal, a Square is a Shape.
Composition is more of "has-a" relationship, like a Car has a Radio
I hope I made it clear, it's a little hard to grasp that reasoning with meaningless class names like A and B :)