Go to Post Even when the system is "really good" it can always be tweaked a little to make it even better. - dlavery [more]
Home
Go Back   Chief Delphi > Technical > Programming > Python
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 17-09-2012, 17:16
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,024
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
"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
... and I want to pass this function as an argument to a library function which is expecting only one argument (i.e "x").

Can I assign constant values to "a" "b" and "c" and have the interpreter literally replace all occurrences with the numerical values?


Reply With Quote
  #2   Spotlight this post!  
Unread 17-09-2012, 18:41
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,024
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: "compiler" constants

Quote:
Originally Posted by Ether View Post
...............click for context^
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" ?


Reply With Quote
  #3   Spotlight this post!  
Unread 18-09-2012, 00:43
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,040
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
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
Functions can actually be treated like variables too.

Code:
>>> def fn():
...     pass
... 
>>> x = fn
>>> x
<function fn at 0x7ffe4c917578>
>>> x.y = 1
>>> x
<function fn at 0x7ffe4c917578>
>>> x.y
1
But, to actually answer your question, this SO post has a reasonable set of answers about Python's scoping rules.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote
  #4   Spotlight this post!  
Unread 18-09-2012, 08:11
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,024
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: "compiler" constants

Quote:
Originally Posted by virtuald View Post
For fun, you can also define functions inside other functions
You can do this in Delphi too. It can be a helpful way to organize code and make it more readable.


Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 19:50.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi