Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Global array in python

1,615 views
Skip to first unread message

Rudolf

unread,
Sep 28, 2009, 8:48:13 PM9/28/09
to
How can i declare a global array in python?

Chris Rebert

unread,
Sep 28, 2009, 9:04:37 PM9/28/09
to Rudolf, pytho...@python.org
On Mon, Sep 28, 2009 at 5:48 PM, Rudolf <yellowbl...@gmail.com> wrote:
> How can i declare a global array in python?

Python has no concept of declarations.
And it doesn't have arrays, it has dynamically-resizing lists.

Some examples:

one_empty_list = []
a_list_of 5 zeroes = [0]*5


Might I recommend you read the Python tutorial? I will help you in
your programming class.

Cheers,
Chris
--
http://blog.rebertia.com

Message has been deleted

Olof Bjarnason

unread,
Sep 29, 2009, 2:01:55 AM9/29/09
to rantingrick, pytho...@python.org
2009/9/29 rantingrick <ranti...@gmail.com>:
> On Sep 28, 8:04 pm, Chris Rebert <c...@rebertia.com> wrote:

>> On Mon, Sep 28, 2009 at 5:48 PM, Rudolf <yellowblueyel...@gmail.com> wrote:
>> > How can i declare a global array in python?
>>
>> Python has no concept of declarations.
>> And it doesn't have arrays, it has dynamically-resizing lists.
>
> What version are you using, i must have py4000 alpha?
>
>>>> import array
>>>> a = array.array('i')
>>>> a.append(1)
>>>> a
> array('i', [1])
>
> hmm? ;-)

Heh. I must remember to never state "python does not have X" - it
almost always has X, I just don't know where yet ;)

> --
> http://mail.python.org/mailman/listinfo/python-list
>

--
twitter.com/olofb
olofb.wordpress.com
olofb.wordpress.com/tag/english

Chris Rebert

unread,
Sep 29, 2009, 2:03:36 AM9/29/09
to rantingrick, pytho...@python.org
On Mon, Sep 28, 2009 at 6:55 PM, rantingrick <ranti...@gmail.com> wrote:
> On Sep 28, 8:04 pm, Chris Rebert <c...@rebertia.com> wrote:
>> On Mon, Sep 28, 2009 at 5:48 PM, Rudolf <yellowblueyel...@gmail.com> wrote:
>> > How can i declare a global array in python?
>>
>> Python has no concept of declarations.
>> And it doesn't have arrays, it has dynamically-resizing lists.
>
> What version are you using, i must have py4000 alpha?
>
>>>> import array
>>>> a = array.array('i')
>>>> a.append(1)
>>>> a
> array('i', [1])
>
> hmm? ;-)

I should have qualified that statement... :)

koranthala

unread,
Sep 29, 2009, 3:43:45 AM9/29/09
to
On Sep 29, 5:48 am, Rudolf <yellowblueyel...@gmail.com> wrote:
> How can i declare a global array in python?

As others have mentioned, you do not have concept of declaration. But
if you are looking for creating a global variable, it is like any
other language. Declare the same in a module, outside any procedures
or classes.

But, please note that it is not completely global. You have to go
inside the modules namespace to get it.
So, it will be like

Module A:
l = []

Module B:
import A
A.l ==> This is the global variable you are looking for.

To explain it further, every variable lives in a namespace. The
namespace can be that of a module (which is the global thing you are
looking for, I guess), or a class/object or even a procedure.

There is also a global keyword etc if that is what you are looking
for. Check out python documentation for more details.

HTH
K

Jean-Michel Pichavant

unread,
Sep 29, 2009, 5:24:05 AM9/29/09
to Rudolf, pytho...@python.org
Rudolf wrote:
> How can i declare a global array in python?
>
import __builtin__
__builtin__.myList = []

Now you can use myList everywhere in your code.

Note that modifying __builtin__ is **not** recommended at all. I don't
have time to detail that point, google it if you want the answer. I
warned you :o)

JM

0 new messages