Fundamental Question

A forum for general discussion of the Python programming language.

Fundamental Question

Postby dday9 » Mon Oct 21, 2013 6:32 pm

I've been working with Python for about two weeks now and I think something has clicked with me. I was preforming functions(in the past) like this:
Code: Select all
cells = ["hello", "world", "what", "is", "up"]

def reset_cells():
   cells = ["_", "_", "_", "_", "_"]


Thinking that, that function would set my variable cells that was outside the function. But recently I've changed my code to look like this:
Code: Select all
cells = ["hello", "world", "what", "is", "up"]

def reset_cells(changing):
   changing = ["_", "_", "_", "_", "_"]
   return changing
   
cells = reset_cells(cells)


So I'm finding that in Python you're not able to set a variable that's outside of a function, is that correct? Or is there a different way of doing what I want that's more acceptable?
dday9
 
Posts: 17
Joined: Fri Oct 11, 2013 8:55 pm

Re: Fundamental Question

Postby Kebap » Mon Oct 21, 2013 6:57 pm

This is a very weird example in what it does to the variables, but yeah you are on to something there, I guess! :mrgreen: In python, you pass variables as arguments to functions, the functions usually calculate something, then return some other value. This value is then received by another variable where the function was called. In contrast, the use of global variables is discouraged. If you want to continue combining objects (cell list with values) with functionality (deleting content of a cell), maybe you can start looking into python classes in some time. :geek:

is there a different way of doing what I want that's more acceptable?

Well, what do you actually want?
Last edited by Kebap on Mon Oct 21, 2013 6:58 pm, edited 1 time in total.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 380
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Fundamental Question

Postby Yoriz » Mon Oct 21, 2013 6:57 pm

In your first piece of code cells is local to the function namespace.
Have a read of this about Python Scopes and Namespaces
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 776
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Fundamental Question

Postby ochichinyezaboombwa » Tue Oct 22, 2013 9:15 pm

As per what you actually do, maybe a function is an overkill. Consider:
Code: Select all
>>> cells = ["hello", "world", "what", "is", "up"]
>>> cells = ["_" for x in range(len(cells))]
>>> cells
['_', '_', '_', '_', '_']
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Fundamental Question

Postby micseydel » Tue Oct 22, 2013 9:18 pm

ochichinyezaboombwa wrote:As per what you actually do, maybe a function is an overkill. Consider:
Code: Select all
>>> cells = ["hello", "world", "what", "is", "up"]
>>> cells = ["_" for x in range(len(cells))]
>>> cells
['_', '_', '_', '_', '_']

Why not
Code: Select all
cells = ["_" for cell in cells]

or even
Code: Select all
cells = ["_"] * len(cells)

? There's usually a better way than range(len()).
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1220
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Fundamental Question

Postby ochichinyezaboombwa » Tue Oct 22, 2013 9:44 pm

Agree; it's just bad habits die hard:-).
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Fundamental Question

Postby micseydel » Tue Oct 22, 2013 9:56 pm

;)
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1220
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 1 guest