|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
"compiler" constants
New to Python. Have what I hope is a simple question. I realize Python is interpreted, but I was wondering if there is something in Python equivalent to "compiler constants". To illustrate with a very simple example, suppose I have a function: Code:
def MyFunc(x)
return a*x^2 + b*x + c
Can I assign constant values to "a" "b" and "c" and have the interpreter literally replace all occurrences with the numerical values? |
|
#2
|
||||
|
||||
|
Re: "compiler" constants
Here is what I found, to my surprise:
I can simply assign values to variables a, b, and c: a = 2 b = 3 c = 1 ... and somehow when I pass MyFunc(x) to the library function as a parameter, it works. It had not expected it to. Does anybody know how this works "under the hood" ? |
|
#3
|
||||
|
||||
|
Re: "compiler" constants
As you've found out, there are no such things as constants in Python.
There are other oddities that people used to other languages find weird at first. No such thing as private variables in a class or module. Just about everything happens to be a variable of some kind. No enums (though there are neat tricks to simulate enums). For fun, you can also define functions inside other functions: Code:
>>> def some_fn(): ... x = 1 ... def _some_other_fn(): ... print x ... _some_other_fn() ... >>> some_fn() 1 Code:
>>> def fn(): ... pass ... >>> x = fn >>> x <function fn at 0x7ffe4c917578> >>> x.y = 1 >>> x <function fn at 0x7ffe4c917578> >>> x.y 1 |
|
#4
|
||||
|
||||
|
Re: "compiler" constants
You can do this in Delphi too. It can be a helpful way to organize code and make it more readable.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|