creating constants in Python 2 [duplicate] - python

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?

Related

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 constructor overload with different number of parameters [duplicate]

This question already has answers here:
How to overload __init__ method based on argument type?
(10 answers)
Python function overloading
(19 answers)
Closed 4 years ago.
I am trying to build a complex number calculator in python. The number will be represented by a class with two fields. The first constructor I want to use looks like this:
def __init__(self,real,imag):
self.real = real
self.imag = imag
but i would like to use another, where the only parameter will be a string representing the whole number:
def __init__(self,string_number):
How to implement the overload that allows to use different number of parameters? I am more used to C++, where such thing was easy.
Thanks a lot!
EDIT:
Question marked as duplicate: I did not find any answer regarding different number of parameters. The error I get when trying to build a new instance of the class is about to few arguments.

Accessing data in variables in class Python 3 [duplicate]

This question already has answers here:
Python: access class property from string [duplicate]
(4 answers)
Closed 4 years ago.
I am using Python 3.5 and wish to do something like this
I have created a class which has variables Bitcoin, Monero , 'Etherum' ,etc with various integer values ,I wish to extract them
var1="Bitcoin"
value=classobj.var1 // there is a class which has a variable called Bitcoin and its value is 10 I wish to get its value using classobject.Bitcoin but the variable called var is Dynamic
print (value)
How do I achieve the same ?
EDIT
I know it is possible using switch statement but I am looking for other ways
This is almost always a bad idea—and you really should explain why your design looks like this, because it's probably a bad design.
But "almost always" isn't "always", so Python has a way to do this:
getattr(classobj, var)

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.

Proper Python Syntax and Semantics: if, else, pass [duplicate]

This question already has answers here:
Using pass on a non necessary else statement
(5 answers)
Closed 8 years ago.
Is there a preferred/proper style?
This:
def fx(Boolean):
if Boolean:
# Do stuff.
else:
pass
Or this:
def fx(Boolean):
if Boolean:
# Do stuff.
Is it preferred/proper to include else: pass if you don't want anything to happen?
I've read PEP 8 - Style Guide for Python Code and did not find anything concerning my question.
You should never include else: pass. It's superfluous. Just omit the else; it's intentionally an optional keyword.
If you don't need the else if there is no reason to add it. It will just confuse other people reading your code in the future (ie yourself a few months later).

Categories

Resources