Python Forum
When do you put variables inside vs outside a function?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When do you put variables inside vs outside a function?
#4
You are trying two different things.
This guy, the instructor, is just lazy.

You have two lists. These lists are not variables but objects, instances of a class in Python.
>>> first_list = [1,2,3,4,5,6]
>>> second_list = [101,202,303,404]
>>> type(first_list)
<class 'list'>
>>> type(second_list)
<class 'list'>
Well, classes are constructions which can contain variables ( instances of predefined classes in Python as integers, lists, strings, etc. ) and functions to operate with these variables.
The variables defined inside a class we call attributes of that class and the functions defined inside a class we call methods.
Each data type class in Python has its own methods. How definition of a class looks? Something simple.
>>> class Greeting:
...     def say(self): # this is a function defined inside a class definition so it's called a method of that class
...         print('Hello!')
... 
>>> greeting = Greeting() # here an instance of a class called 'greeting' is created 
When you define a variable in Python as a list, for example, it happens basically the same but the syntax is a bit different and Python is doing its things behind so you don't see it.
first_list = [1,2,3,4,5,6] # 'first_list is an instance of a class list
As I showed you at the beginning the lists are just classes in Python. Like the integers or all the other data types in Python. Everything in Python. Big Grin

So this instance of the class list has its own methods. Calling a method is different from calling a function. Let's define a simple function.
>>> def say(object):
...     print(object)
... 
Now we have a method of the class Greetings called 'say' and a function 'say'.

Calling a function is simple.
>>> say(first_list)
[1, 2, 3, 4, 5, 6]
Calling a method of a class ( or instance of a class ) is simple too. But we need to know whose class/instance is this method.
We doing it as putting the class/instance along with its method, separated by a period.
>>> greeting.say()
Hello!
As you may already guess when you reverse a list the first way ( the working one ) you just call its method 'reverse'.
So it becomes:
>>> first_list.reverse() # calling the method 'reverse' of the instance of a class list, called 'first_list'
>>> first_list
[6, 5, 4, 3, 2, 1]
In order to do the same using a function, you have to create such a function. But you didn't do that, so this function doesn't exist. And got an error.

However, there is a built-in function in Python called 'reversed'. Not 'reverse'.
>>> reversed(first_list)
<list_reverseiterator object at 0x7f7184183dd8>
It looks like it doesn't work too. We don't get the reversed list. But it works. This function returns an iterator. It says in the output what it is. An iterator from a class list called reversed and the memory address follows. The iterators are another concept in Python.
Instead of the whole list or a sequence, they return a single element. It saves memory. For the examples we are using here it doesn't matter because the lists are small. But nowadays almost nothing is small when we are talking about data.

You have to pass the iterator to a for loop for instance or to a function which knows what to do with it.
>>> list(reversed(first_list))
[6, 5, 4, 3, 2, 1]
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
RE: When do you put variables inside vs outside a function? - by wavic - Jun-02-2018, 06:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  with open context inside of a recursive function billykid999 1 606 May-23-2023, 02:37 AM
Last Post: deanhystad
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 841 May-02-2023, 08:40 AM
Last Post: Gribouillis
  How to print variables in function? samuelbachorik 3 955 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  User-defined function to reset variables? Mark17 3 1,706 May-25-2022, 07:22 PM
Last Post: Gribouillis
  How to make global list inside function CHANKC 6 3,168 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  Parameters aren't seen inside function Sancho_Pansa 8 2,987 Oct-27-2020, 07:52 AM
Last Post: Sancho_Pansa
  Do I have to pass 85 variables to function? Milfredo 10 4,379 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  Creating a variables inside FOR loop zazas321 5 4,163 Sep-16-2020, 04:42 PM
Last Post: Naheed
  print function help percentage and slash (multiple variables) leodavinci1990 3 2,531 Aug-10-2020, 02:51 AM
Last Post: bowlofred
  Issues with storing variables outside of a function cerulean747 7 3,790 Apr-30-2020, 08:46 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020