This question already has answers here:
What is the naming convention in Python for variable and function?
(15 answers)
Closed 2 years ago.
Any tips on how I should properly be naming python variables? Like any correct forms or for clean code.
Like for instance
myNum = 1
or
mynum = 1
or
my_num = 1
PEP 8 recommends my_num. #Merp's comment is correct though. Be clear and consistent with whatever convention you choose.
Well, traditionally developers follow the 'camelCase' variable naming proceedure in most languages, which is basically shown in your 1st code snippet. However, there are exceptions in the wide range of programming languages out there in terms of norms that manifest themselves over time. In Python, most developers tend to use the variable naming shown in your 3rd code snippet, separating each word with an underscore.
Related
This question already has answers here:
What determines which strings are interned and when? [duplicate]
(3 answers)
About the changing id of an immutable string
(5 answers)
Closed 9 months ago.
I am fairly ok with Python, but recently I learned something that made me question everything I thought to be true in my life :D
So if I run this:
w1 = "word"
w2 = "word"
print(id(w1))
print(id(w2))
To my utter horror, the output is:
140675515277936
140675515277936
I always believed that as strings are immutable, they will have their own address. I was wrong. My question then:
How does the Python Memory Manager find existing string (or any) objects when a new variable is created, so it can make that new variable point to them?
With some hashing? If besides your answer you can point me to learning material about this, i will be double grateful! Cheers!
This question already has answers here:
How to evaluate environment variables into a string in Python?
(3 answers)
Closed 2 years ago.
I have the following string :
some_string = "%envvar1%\location\execution.exe"
envvar1 is an environment variable with value of "c:\", and I would like some function as following:
some_string = "%envvar1%\location\execution.exe"
inject_env_variable(some_string)
print(some_string)
"c:\location\execution.exe"
Creating a function like this wouldnt be to difficult with regular expressions and os.environ but I was wondering if there was some kind of built in module that treats these kind of things.
Note: google searching anything with the word 'path' and 'python' is really tedious since all the searches are related to pythonpath :P
os.path.expandvars is probably what you are looking for. https://docs.python.org/3/library/os.path.html#os.path.expandvars
import os
def inject_env_variable(s):
return s.replace("%envvar1%", os.environ['envvar1'])
Should do the trick
This question already has answers here:
Python Named Argument is Keyword?
(2 answers)
Closed 6 years ago.
I'm writing a python script wherein I need to use the continue word.
I know that it is a python keyword.
So how should I write my script so that python will not complain that I am using a keyword as a string literal.
Thanks in advance for your time.
You should use something like:
_continue
But why not using more descriptive and longer variable name?! like:
continue_whatever or go_on ...
The normal practice is to use a trailing underscore to use a keyword that already being used by the language, like continue_ or input_.
I don't think anyone would think that just overwriting keywords is every a good idea.
This question already has answers here:
Dynamic variable in Python [duplicate]
(2 answers)
Closed 8 years ago.
I would like to know how to create lots of variables by looping it. I know other people have asked this before but everyone who knows says you need a good reason for it and to just set it in a dictionary. My reason is that I need to assign up to 6156119580207157310796674288400203776 variables and there is no way I can do that by typing them out.
I need something like:
while counter < 1000:
try[counter] = counter
So that I could do this:
>>> try837
837
>>>try453
453
etc.
(this is an example not the exact code but any answer for this will solve my problem)
I would also like to know why people are opposed to answering this particular question. I don't want to tax my computer more than I already am by assigning this many variables so if it is an issue that could harm my computer or my code I would like to know.
You don't want to do this. Create a dictionary with a key for each suffix that you would use. Then use try[557] in place of the variable try557.
>>> try_ = dict((counter, counter) for counter in range(1000))
>>> print try_[557]
557
I'm using the standard technique of affixing an underscore to the otherwise reserved word "try".
(I'm ignoring the ludicrously large number of variables you claim to need.)
This question already has an answer here:
How to choose proper variable names for long names in python
(1 answer)
Closed 5 years ago.
What is the PEP8 correct way for long method's name?
I have a unit test with a self-describing method:
def success_if_buying_price_item_when_participating_and_progression_is_100_percent(self):
But unfortunately this (too long?) method reaches the 80 characters line limit.
Should I rename it and add a description in code or there is an other way?
Should I rename it and add a description in code or there is an other
way?
Yes, rename it and add a description. This code is dangerous, one could fall from its chair while another could have a heart attack. Please, rename that ASAP, you don't want to feel responsible.