Tags: declare, declaring, def, foo, foo1the, global, listi, nestedfunction, programming, python, size, sodef, specific

declaring list in python

On Programmer » Python

2,035 words with 1 Comments; publish: Sun, 04 May 2008 20:54:00 GMT; (20066.41, « »)

Hello list

I want to declare a list of a specific size as global to some nested

function like so

def foo (n):

A[] (of size n)

def foo1

The only way I can think to declare the list is to use dummy values:

A = [0] * n

A = [] * n doesn't work. [] * n = []

I'd prefer not to use dummy values I have no use for. Is there any way?

Thanks

Logesh Pillay

Tutor maillist - Tutor (AT) python (DOT) org

All Comments

Leave a comment...

  • 1 Comments
    • Logesh Pillay said unto the world upon 10/01/06 11:28 PM:

      Hello list

      >

      I want to declare a list of a specific size as global to some nested

      function like so

      Hi Logesh,

      what problem are you trying to solve by doing this? Knowing that will

      help generate more useful answers, I suspect.

      def foo (n):

      A[] (of size n)

      def foo1

      >

      The only way I can think to declare the list is to use dummy values:

      A = [0] * n

      >

      A = [] * n doesn't work. [] * n = []

      A = [None] * n

      would be a better way to created an n-placed list of dummy values in

      Python, I think.

      I'd prefer not to use dummy values I have no use for. Is there any

      way?

      This is why knowing your problem would be helpful. Built-in Python

      data structures don't have size limitations that are declared when an

      instance of the data structure is created. (Python's not C.) There is

      no way (that I know of) to create an n-placed list save creating a

      list with n objects.

      So, I think you will have to either give up on not employing dummy

      values or give up on creating a list of a fixed length.

      You could subclass list to create a class with a max. and/or min.

      length, but I think knowing more about what you want to do would be

      helpful before getting into that :-)

      Best,

      Brian vdB

      Tutor maillist - Tutor (AT) python (DOT) org

      #1; Sun, 04 May 2008 20:55:00 GMT