assign a variable with more than one capital letter - python

I think that my question is very stupid. I'm using windows / Python 3.7 / spyder (just updated)
If I write in the terminal g=3 and press enter, I get a variable named g in the variable explorer, if I say GA=3, nothing appears in the variable explorer but if I write GA in the terminal the variable is there. I have no idea what is happening here. It only happens when the variable name has more than one capital letter.

From their github issues
For example entering the following at the commandline: TESTVAR = 2
will not show up in the workspace, although testvar = 2 will show up.
That's because there is an option to hide this kind of variable which
should be (according to Python official guidelines) a global variable
in an imported module (imported with the non-recommended statement
"from foobar import *". Anyway to show these variables, just tick the
option "show upper case [something]" (context menu)
So, there in an options icon in the vertical bar at the right of the Variables explorer which allows you to enable/disable the uppercase variables.
Note: "Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL", according to PEP-8. And it is probably this very convention that Spyder follows that caused the behaviour you observed.

Related

python breakpoint() default entry point is one level down than expected

I am having a strange problem with breakpoint() that I have never encountered before. It is stepping into the program a level down than it usually does. I can recover the previous default behaviour by pressing 'u'.
I am using python 3.9, conda environment in pycharm 2021.3.1
For example:
def hello():
return "hello"
x = 1
breakpoint()
hello()
Will start the debugger from within the function hello(). If I type 'x' I will not be able to see the variable, as am in the function workspace. Pressing 'u' allows me to get back to where breakpoint(), when working as expected, used to break into.
I must have changed the default somewhere but I cannot for the life of me find where, I seem to have no PYTHONBREAKPOINT environment variable set...

PyCharm keyword argument

How to get PyCharm to show the keyword arguments while coding, like the options you can pick from? I can get VSCode to show this normally, but I just can't figure out how to get PyCharm to do it. You'll see the difference in the screenshots. One is from PyCharm and the other one is from VSCode
If you start a new project in PyCharm, add a .py file and entire the following code:
import os
os.chdir()
And you position the cursor between the two parentheses while typing, or when you press Ctrl+P in that position, PyCharm will show a hint saying "path: int | str | bytes | PathLike[str] | PathLike[bytes]", like this:
If you're just looking for the autocomplete, instead of the type hints, similarly ensure you're typing something that could be completed and PyCharm wil show type hints you can navigate with arrow keys (or can just complete by hitting Enter).
Note that you can cause PyCharm to show it again by hitting Ctrl+Space.
Mind you, if you type something that has no logical completion (i.e. nothing starts with the string you've written so far), there is no autocompletion, and so none is shown - instead PyCharm will show a small message in its place saying 'no suggestions'.

Add functions to path - Python

I have just started to use Python (I am used to MATLAB).
How should I add a function to path? In matlab you right click on the function and you can add or remove from path.
For instance, a simple program to evaluate the power of a number:
As you can see the original program and the function are in the same folder, however the function seems to have an error. What am I doing wrong?
Thanks
If you hover the mouse over the redlined identifier, PyCharm will tell you what it thinks is wrong (and give you a balloon to fix it). Alternatively you can press F2 to go to the next error.
In this case it is complaining that you're not following PEP8 conventions for function and argument names.
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
Variable names follow the same convention as function names.
From: https://www.python.org/dev/peps/pep-0008/#function-and-variable-names
In addition, you need to import names before you can use them, ie. in app.py:
from . import POWER
a = 3
print(POWER.POWER(a))
or
from .POWER import POWER
a = 3
print(POWER(a))
Since you're using PyCharm, you can put your cursor on the name in app.py that is not recognized and press alt+enter and chose the option to "import this name".
Hi function in python define like this :
def <funcname>(<arguments>):
statement....
If you want to call function use:
<funcname>(<arguments>)
your fucntion:
def power(num):
return num**2
print(power(2))
output:
4
but if you want to add it to another .py file
write this code in your app.py
from .power import power
print(power(2))
Python has a built-in hierarchy of import protocols that I would suggest getting to know. To accomplish what you're trying to do, if you (after cleaning up a bit of syntax, and remove the space between POWER and the brackets, and add a colon to the end of the function definition, ie def power(Num): ) you can access the function from file POWER by adding to the top of app:
from POWER import power

PyInstaller: Executable to access user-specified Sourcecode

I'm using pyinstaller to distribute my code as executable within my team as most of them are not coding/scripting people and do not have Python Interpreter installed.
For some advanced usage of my tool, I want to make it possible for the user to implement a small custom function to adjust functionality slightly (for the few experienced people). Hence I want to let them input a python file which defines a function with a fixed name and a string as return.
Is that possible?
I mean the py-file could be drag/dropped for example, and I'd tell them that their user-defined function needs to have a certain name, e.g. "analyze()" - is it now possible to import that from the drag/dropped pythonfile within my PyInstaller Script and use it as this?
I know, it certainly will not be safe/secure and they could do evil things, delete files and so one... But that are things which we don#t care at this point, please no discussions about it. Thanks!
To answer my own question: yes it does actually work to import a module/function from a given path/pythonfile at runtime (that I knew already) even in PyInstaller (that was new for me).
I used this for my Py2.7 program:
f = r'C:\path\to\userdefined\filewithfunction.py'
if os.path.exists(f):
import imp
userdefined = imp.load_source('', f) # Only Python 2.x, for 3.x see: https://stackoverflow.com/a/67692/701049
print userdefined # just a debugging print
userdefined.imported() # here you should use try/catch; or check whether the function with the desired name really exists in the object "userdefined". This is only a small demo as example how to import, so didnt do it here.
filewithfunction.py:
--------------------
def imported():
print 'yes it worked :-)'
As written in the comments of the example code, you'll need a slightly different approach in Python 3.x. See this link: https://stackoverflow.com/a/67692/701049

Is is possible to customize how comments are inserted via the Keyboard Shortcut?

Is it possible to change the way PyCharm adds the hash # when using the keyboard shortcut to comment a line? (default CTRL+/)
I'd like the # to be at column 1 rather than at the indent level. Also, the cursor is moved down a line after the keyboard shortcut and I'd rather it stay on the same line.
Currently:
def foo():
my_uncommented_line
# commented_with_keyboard_shortcut
var = "and now the cursor is at the start of this line"
What I'd like:
def foo():
my_uncommented_line
# commented_with_keyboard_shortcut
var = "cursor stays on the previous line"
I'm currently searching around the JetBrains plugin repo for something that does this, but no luck thus far.
I know that this doesn't follow PEP8. I'd like to be able to make this change so that I can be consistent with the rest of the project:
Some other good reasons to ignore a particular guideline:
2. To be consistent with surrounding code that also breaks [the guideline].
The most you can do is alter the style:

Categories