All Questions
17 questions
-1
votes
2
answers
827
views
How to decide if a global variable is used inside or outside a function in Python?
In Python variables that are created outside of a function are known as global variables. However to create a global variable inside a function (a local variable), you can use the global keyword.
My ...
3
votes
1
answer
379
views
Should I rename variables that are already constants in my own library?
I'm writing a high level wrapper around the Python socket.socket object. Specifically, I want to do this for IPv4 TCP sockets (though it would be useful to be able to expand the library later with ...
32
votes
3
answers
15k
views
Why should I use namedtuple over SimpleNamespace when not using dict, they seem very similar
At one point or another you might come over functions with a lot of arguments. Sometimes it makes sense to combine some of the arguments into super-arguments. I've often done this with dicts, but now ...
1
vote
2
answers
2k
views
Using "intermediate variables" to abbreviate long dict entries
Sometimes I come across the situation that I have some data in a nested dict with rather long key-names (sometimes unavoidable for one reason or another).
some_dict = {'not_too_short_key_one': {'...
3
votes
2
answers
1k
views
How to name variables that clash with functions?
As an example, in Python I have a lowest_prime_factor function which does it exactly what it says it does. Then, in another function, I need to call this function and store it. In another language ...
0
votes
2
answers
229
views
Variable can't hold str() method information with "pure" word in brackets... why?
I've recently bumped into string methods and mainly
str()
method caught my interest because, it makes string out of every type of data?
integers, booleans and so on. And if you want, they can be ...
16
votes
2
answers
9k
views
Is there a convention for returning multiple items?
In Python specifically (I don't know if this generalizes) is there a "best" way to return multiple items from a function?
def func1():
return a,b #equivalent to (a,b)
def func2():
return[a,...
9
votes
1
answer
9k
views
Why are variables in Python different from other programming languages'? [closed]
According to what I know, a variable in Python is a name that refers to a value stored in the computer memory, like a label on a box.
but in other programming languages a variable is a location, in ...
5
votes
1
answer
3k
views
Patterns for sharing context variables between functions
I am looking for ways of passing around a set of contextual variables that are required by functions.
As a Python programmer, right now I can see three ways of solving the problem: passing them ...
2
votes
1
answer
359
views
Python beginner question with Assigning Variables
Coming from C# and now getting my hands dirty with Python.
From what I understand, Python is strongly typed:
https://stackoverflow.com/questions/11328920/is-python-strongly-typed
As stated in the ...
2
votes
1
answer
13k
views
Python: Faster to use global variable or pass as arguments to a function? [duplicate]
Hey so i was wondering which is the more efficient way, or better practice to do in this situation.
1,
def function():
global number
number += 2
Or 2,
def function(number):
return ...
8
votes
4
answers
5k
views
Prime symbol in Python variable name
So I'm a terrible person and I want to name a variable in my mathy-python3 code s′ (that's U+2032 PRIME).
I was under the impression Unicode literals work as identifiers in Python 3, which is why my ɣ,...
2
votes
2
answers
5k
views
Is there a way to prevent variables from changing their type in Python?
It is useful to have the interpreter derive the type of a variable automatically. This on its own is similar to the auto keyword in C++11. However, in Python variables can change their type after ...
0
votes
1
answer
84
views
Should instance variables be bound to the instance right in the beginning of the method they are created in?
A common problem for me - and it's not really a problem, but somehow I again and again ask myself, what is the normal way to handle this - is that there is a method and within this method a couple of ...
5
votes
8
answers
26k
views
Should I store x,y coordinates as an array, a class object, or two variables?
I have a MyObject which has an x and y coordinate.
as far as I can see, I can store it in three ways:
class MyObject:
def __init__(self, x, y):
self.x = x
self.y = y
class ...