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?
#7
Thank you @buran for your concise and to the point answer. And a thousand thank-you’s go out to @wavic: I wholeheartedly appreciate the time and care you put into your answer to my question. Feedback from the both of you has helped.

I think I am beginning to understand some of this.

reverse() is a class method used to manipulate class objects such as lists. To reverse the order of a list, the variable goes “outside” (before the method separated by a dot).

To invoke the reversed() function to manipulate a declared list variable and to create the same effect, the variable goes "inside" as an argument. But to do so successfully, it’s necessary to wrap it around a list() function.

Next I’ll look into the concept of iterators in Python.

(Jun-02-2018, 05:40 AM)buran Wrote: Now to the question - you will know what to do by reading the documentation for the package/module you want to use - e.g. does it provide function(s) or is it a class(es) and has some methods...

As @wavic pointed out, to learn about what certain class methods can be used to manipulate objects and for quick reference, I can leverage dir(), type() and help() around given objects in my interpreter, right?

Obviously there are docs on Python’s official website and resources around the web also.

Thanks again you two.
Reply


Messages In This Thread
RE: When do you put variables inside vs outside a function? - by Drone4four - Jun-07-2018, 12:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  with open context inside of a recursive function billykid999 1 589 May-23-2023, 02:37 AM
Last Post: deanhystad
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 801 May-02-2023, 08:40 AM
Last Post: Gribouillis
  How to print variables in function? samuelbachorik 3 918 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  User-defined function to reset variables? Mark17 3 1,662 May-25-2022, 07:22 PM
Last Post: Gribouillis
  How to make global list inside function CHANKC 6 3,123 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  Parameters aren't seen inside function Sancho_Pansa 8 2,958 Oct-27-2020, 07:52 AM
Last Post: Sancho_Pansa
  Do I have to pass 85 variables to function? Milfredo 10 4,326 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  Creating a variables inside FOR loop zazas321 5 4,110 Sep-16-2020, 04:42 PM
Last Post: Naheed
  print function help percentage and slash (multiple variables) leodavinci1990 3 2,500 Aug-10-2020, 02:51 AM
Last Post: bowlofred
  Issues with storing variables outside of a function cerulean747 7 3,739 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