Python Forum
const or #define coming from C
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
const or #define coming from C
#1
please, coming from C, where would be #define or const int ... to a newbie just scraping the surface of python?

for example, defining:

#!/usr/bin/python3

i = 3

print (i)
Reply
#2
The above works, though you should not have a space after print, but you are assigning to a variable.
In truth, we do not use constants in Python. There is a convention - defining things you mean to be constant in all caps and with underscores between words - but that does not prevent reassignment. So,
PI = 3.1415926535
TWO = 2
print(PI*TWO)
But again, all caps does not prevent reassignment, rather it is a convention so that if you are tempted to reassign the value you think twice.
Reply
#3
(Sep-14-2021, 04:02 PM)jamie_01 Wrote: please, coming from C, where would be #define or const int ... to a newbie just scraping the surface of python?

There is a big difference between #define and const int. When I search for preprocessors for Python I found How would you do the equivalent of preprocessor directives in Python? | Newbedev.
Reply
#4
But you would never use PI = 3.1415926535
import math
TWO_PI = 2 * math.pi
You should define python global "constants" the same way. Put all your global constants in one file and import that file to access the constants.

In a file named constants.py
import math

TWO_PI = 2 * math.py
In a file where you want to use TWO_PI
import constants

circumference = radius * constants.TWO_PI
You would only do this if you had multiple constants that are used in multiple files.
Reply
#5
I think you should decide whether you want to write Python or C. What I mean is, instead of just trying to translate things you'd do in C to Python, take the time to learn Python's idioms and then use them to do things the Pythonic way.
buran likes this post
Reply
#6
(Sep-15-2021, 08:05 AM)ndc85430 Wrote: instead of just trying to translate things you'd do in C to Python, take the time to learn Python's idioms and then use them to do things the Pythonic way.
I did not ask the question but I am interested in knowing how to define or declare a name in Python that is guaranteed to not change later. I searched and all I found was answers saying that Python does not do that. If it cannot be done using Python then there is not a Pythonic way.
Reply
#7
I think he means bigger tasks. Python does not use constants, so you learn a pythonic method for doing whatever it is you needed the constant for. Or, you follow the convention and remember not to change variables that have all caps. Because...
import math
math.pi = 3
print(math.pi)
Output:
3
So, think of another way to handle the problem rather than use constants.
Reply
#8
(Sep-15-2021, 08:05 AM)ndc85430 Wrote: take the time to learn Python's idioms and then use them to do things the Pythonic way.
The implication of that is that it is foolish to want constants for Python. Is that what the Python experts think?
Reply
#9
Yes
Though, truly I cannot speak for the guidance committees. Suggest a PEP that adds constants and see what the reaction is.
Reply
#10
If you really, really, really want to have Python constants you can do something like this:
import math

class ConstantMeta(type):
    def __setattr__(cls, attr, value):
        raise ValueError('You are not allowed to set constants')

class Constant(object, metaclass = ConstantMeta):
    two_pi = 2.0 * math.pi


print(Constant.two_pi)
Constant.two_pi = 3
Output:
6.283185307179586 Traceback (most recent call last): File "...", line 15, in <module> Constant.two_pi = 3 File "...", line 5, in __setattr__ raise ValueError('You are not allowed to set constants') ValueError: You are not allowed to set constants
The metaclass catches attempts to set a class attribute and raises a ValueError.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to know coming snmp trap version in python pysnmp? ilknurg 0 2,624 Jan-31-2022, 12:16 PM
Last Post: ilknurg
  String coming up weird EddiesTech 1 1,534 Mar-15-2020, 05:02 PM
Last Post: buran
  Timer keeps coming up with a non-callable int object birdwatcher 1 3,177 Feb-16-2020, 12:40 PM
Last Post: DeaD_EyE
  Error Message Coming Up When Running Code eddywinch82 0 1,894 Feb-10-2020, 11:48 PM
Last Post: eddywinch82
  need help coming up with a code Staph 0 1,463 Jun-21-2019, 10:06 AM
Last Post: Staph
  Learning python My lists are not coming out right TheMusicMan 3 2,621 Aug-17-2018, 02:45 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020