List of class instances in python [duplicate] - python

This question already has answers here:
Python copy.deepcopy() function not working properly [duplicate]
(3 answers)
How to avoid having class data shared among instances?
(7 answers)
Closed 5 years ago.
I am new to python, and more used to C++. I want to create a list of instances and did the following:
from copy import deepcopy
class C:
c1=""
c2=""
Cs=[]
C.c1="Hello"
C.c2="World"
Cs.append(deepcopy(C))
C.c1="Why"
C.c2="this?"
Cs.append(deepcopy(C))
for c in Cs:
print (c.c1, c.c2)
I expected the following output:
Hello World
Why this?
but got:
Why this?
Why this?
Why is the deep copy not working?

there is only one (static in the Java/C++ sense) copy of the c1 and c2 variables. Read https://www.toptal.com/python/python-class-attributes-an-overly-thorough-guide and sprinkle more selfs in your code to fix it.

Related

What is the difference between shallow copy and '=' in Python? [duplicate]

This question already has answers here:
What is the difference between shallow copy, deepcopy and normal assignment operation?
(12 answers)
Closed 3 years ago.
x = Object1
b = Object2
What is the difference between
import copy
x = b
x = copy.copy(b)
Assignment statements in Python do not copy objects, they create bindings between a target and an object
copy doc #python.org
Thus if you really want to have the same object twice without any bindings between you can use the copy package.

Python class OOPs concept [duplicate]

This question already has answers here:
What is the difference between old style and new style classes in Python?
(8 answers)
Closed 3 years ago.
Snippet 1:
class Room(Object):
pass
Snippet 2:
class Room():
pass
What is the different between both of the code and why we write object in the above code?
Both are the same in Python3.
Read more about it in Python's documentation.
You might want to read this stack overflow question as well.
Search old style classes vs new style classes in Python.

Python dot notation method attributes VS other methods [duplicate]

This question already has answers here:
Why isn't the 'len' function inherited by dictionaries and lists in Python
(7 answers)
Why does Python code use len() function instead of a length method?
(7 answers)
In Python, when should I use a function instead of a method?
(5 answers)
Difference between len() and .__len__()?
(5 answers)
Closed 4 years ago.
In Python, why are some built-in functions called using brackets with the method name before the object, e.g.
print("foobar")
bool("foobar")
...
While others are built-in method attributes, called with a dot behind the object, e.g.
"foobar".capitalize()
Specifically I'm interested to learn if there is a general principle behind this instead of just common practice and memorization. In cases where you can't quite remember whether it was capitalize("foobar") or "foobar".capitalize(), how do you know?

How to "hint" the type of an empty list in python [duplicate]

This question already has answers here:
Type hinting a collection of a specified type
(5 answers)
Pycharm: Type hint list of items
(2 answers)
Closed 4 years ago.
I have a Class:
class MyClass(object):
def __init__(self):
self._subscribers = []
and i want to specify the type that will go into the list 'self._subscribers'. for example a queue.Queue() object. but the list should still be an empty list.
just for ease of use in PyCharm
self._subscribers = [queue.Queue]
self._subscribers = list[queue.Queue]
both don't seem to work
thanks
PS.: Python 3.64

creating constants in Python 2 [duplicate]

This question already has answers here:
How can I represent an 'Enum' in Python?
(43 answers)
How do I create a constant in Python?
(44 answers)
Closed 6 years ago.
This question was marked as duplicate of another question. I am not asking how to create enumerations, I'm asking how to create constants, and that is the difference between the questions.
This question was marked as duplicate of yet another question. While the answers do explain how to create constants in python they don't explain how to create them in Enum like style, which is what my question is asking for.
I need to create constants in my python code. I don't want the user to be able to modify them. I found this, but it is not exactly what I need. I need to be be able to use them just like enums, without creating and instance.
For example:
class MyConstClass(object):
Const0 = 0
Const1 = 1
Const2 = 2
myfunc(MyConstClass.Const0)
MyConstClass.Const1 = 20 # Raise an exception
I also don't want to create a module, just like it is done here
Is there a way to do it?

Categories

Resources