Python class OOPs concept [duplicate] - python

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.

Related

what does __new__ do in python class [duplicate]

This question already has answers here:
What's ```__new__``` by default in Python 3?
(2 answers)
__new__ and __init__ in Python
(1 answer)
Why not just __new__ and not __new__ and __init__ in python?
(2 answers)
Closed 7 months ago.
in the class concept of python, it states that __init __ initializes the class, which I understood as setting defaults to the class.
However, I bumped into a new concept called __new __, which states that it is used to control the creation of the class.
I have seen several explanations saying that __new __ also returns an instance, but I am also confused about what that is returning and to where.
Can someone clarify these points, please?

Use of None and self keywords in method construction [duplicate]

This question already has answers here:
What does -> mean in Python function definitions?
(11 answers)
Python3 function definition, arrow and colon [duplicate]
(3 answers)
What does the -> (dash-greater-than arrow symbol) mean in a Python method signature? [duplicate]
(1 answer)
Closed 9 months ago.
I'm analyzing some old code that I've inherited, and I have a question about the use of "self" and "None" keywords, specifically in the following example:
def run(self) -> None:
I understand that the self keyword is similar to the "this" keyword in C++ in that, in conjunction with the dot operator, it allows us to access the attributes and methods of the class in question. What I'm really interested in is the use of "-> None" in the declaration of the method named "run." Is this in PEP 8 because I can't find an example. I'm using Python 3.7, in case that matters.
What is the purpose of writing a method in this manner? What does "-> None" do?
They're called type hints, and they enable annotating the types of the parameters and return types of functions.
https://peps.python.org/pep-0484/

List of class instances in python [duplicate]

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.

What does it mean for a method to be a classmethod and what does parameter * represent? [duplicate]

This question already has answers here:
Bare asterisk in function parameters?
(6 answers)
What is the purpose of class methods?
(18 answers)
Closed 5 years ago.
Looking into the Python documentation, I came across the following page:
https://docs.python.org/3/library/stdtypes.html#int.from_bytes
Here, the third argument of int.from_bytes() is *. What does * represent and why can it be ignored in the examples?
Also, the fromkeys(seq[, value]) method is said to be a classmethod, but what exactly does this mean? What class does this method belong to?

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