Re: [Tutor] dictionaries, objects and scoping...

2008-01-22 Thread Tiger12506
Just a thought~

The built-in id() function can be useful in helping to sort out stuff. 
Returns a unique identifier for each object created so you can test whether 
a different name is a different object or just a different name for the same 
object. (This is what the  'is' operator does... Note: the 'is' operator and 
the '==' operator are not the same) 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-22 Thread Alan Gauld
"John Morris" <[EMAIL PROTECTED]> wrote

> So this seems like it will make scope/namespaces a bit 
> interesting...

namespaces in python are literally that, they are spaces
where *names* are visible. Objects are something else
entirely and assignment only pins a name to an object.

So in Python namespaces contriol where you can use a
name, not where you can use an object.

def f(y): return  y+1

x = 66
print f(x)

y is a name that is only visible inside f.
66 is a number object associated with x
and passed into f where it is associated
with y. The number object inside f is the
same object outside f only the name has
changed.The return value is a new number
object (67 in this case).

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Morris
Thanks. I think this is understood better now.

Thanks to everyone for their help. I was running low on ways to express this
for clearer understanding.

Awesomeness once again from [EMAIL PROTECTED]

- John

On Jan 21, 2008 10:18 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:

> John Morris wrote:
> > Ahhh. so namespaces are scoped, not objects...
>
> I would say names are scoped, but I guess namespaces are too.
>
> > You have to keep your objects separate (and make copies when needed),
>
> Yes
>
> > Python just keeps namespaces (names that refer to an object) scoped
> > according to it's rules:
> > http://docs.python.org/ref/naming.html
> >
> > So if you create an object way up in terms of scope (global), then all
> > python does is handle what names are available in a given scope to refer
> > to it. If you want a separate object you have to take care of that
> > yourself. Efficient.
>
> Yes. Assignment is not copying, it is name-binding.
>
> > Massive potential for gotchas, especially with some
> > of python's cleverness in terms of scoping rules.
>
> In my experience it is rare to actually need a copy of an object, and
> usually pretty clear when I do. The real hurdle is getting your mental
> model in sync with what Python is actually doing, rather than what you
> think it should be doing.
>
> Kent
>



-- 
John Morris
[EMAIL PROTECTED]
"Do nothing which is of no use." -- Miyamoto Musashi
http://profile.mygamercard.net/nerdality";>
http://card.mygamercard.net/gbar/abyss/nerdality.gif"; border=0>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread Kent Johnson
John Morris wrote:
> Ahhh. so namespaces are scoped, not objects...

I would say names are scoped, but I guess namespaces are too.

> You have to keep your objects separate (and make copies when needed), 

Yes

> Python just keeps namespaces (names that refer to an object) scoped 
> according to it's rules:
> http://docs.python.org/ref/naming.html
> 
> So if you create an object way up in terms of scope (global), then all 
> python does is handle what names are available in a given scope to refer 
> to it. If you want a separate object you have to take care of that 
> yourself. Efficient. 

Yes. Assignment is not copying, it is name-binding.

> Massive potential for gotchas, especially with some 
> of python's cleverness in terms of scoping rules.

In my experience it is rare to actually need a copy of an object, and 
usually pretty clear when I do. The real hurdle is getting your mental 
model in sync with what Python is actually doing, rather than what you 
think it should be doing.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Fouhy
On 22/01/2008, John Morris <[EMAIL PROTECTED]> wrote:
> So if you create an object way up in terms of scope (global), then all
> python does is handle what names are available in a given scope to refer to
> it. If you want a separate object you have to take care of that yourself.
> Efficient. Massive potential for gotchas, especially with some of python's
> cleverness in terms of scoping rules.

There is potential for gotchas, but it's reduced by the fact that
integers and strings are both immutable.  For example:

>>> x = 'foo'
>>> y = x
>>> y is x   # this tests whether x and y are different names for the
same object
True
>>> y += 'bar'  # this is equivalent to: y = y + 'bar'
>>> y is x
False
>>> y, x
('foobar', 'foo')

(this is why there is no '.append()' method for strings)

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Morris
Ahhh. so namespaces are scoped, not objects...
You have to keep your objects separate (and make copies when needed), Python
just keeps namespaces (names that refer to an object) scoped according to
it's rules:
http://docs.python.org/ref/naming.html

So if you create an object way up in terms of scope (global), then all
python does is handle what names are available in a given scope to refer to
it. If you want a separate object you have to take care of that yourself.
Efficient. Massive potential for gotchas, especially with some of python's
cleverness in terms of scoping rules.

Sort of. I see some light beginning to dawn ;).

On Jan 21, 2008 9:16 PM, John Morris <[EMAIL PROTECTED]> wrote:

> class Foo:
>   '''Represents a foo'''
>   def __init__(self, name):
> '''Initializes the person's data'''
> self.name = name
> print '(Initializing %s)' % self.name
> self.ot = Bar(self.name)
> print '(After Other - %s)' % self.name
>
> class Bar:
> def __init__(self, name):
> self.name = name
> print 'Other', self.name
> self.name.pop('srv')
> print 'Other (Changed)', self.name
>
> dict = { "srv" : "why", "goo" : "sticky" }
> foo = Foo(dict)
> print foo.name
>
>
> Why does the pop in the Bar class nuke the srv k & v from Foo.name as
> well?
>
>


-- 
John Morris
[EMAIL PROTECTED]
"Do nothing which is of no use." -- Miyamoto Musashi
http://profile.mygamercard.net/nerdality";>
http://card.mygamercard.net/gbar/abyss/nerdality.gif"; border=0>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread Kent Johnson
John Morris wrote:
> Thanks,
> 
> so I could/should do
> 
> self.ot = Bar(self.name.copy()) instead

Yes, if you want a copy you have to ask for it.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Morris
So this seems like it will make scope/namespaces a bit interesting...

Any good references on why this is this way?
I.e., why assignment passes across scopes instead of copy.
Or is it just explicit versus implicit?



On Jan 21, 2008 9:32 PM, John Fouhy <[EMAIL PROTECTED]> wrote:

> On 22/01/2008, John Morris <[EMAIL PROTECTED]> wrote:
> > I thought each class got it's own namespace and this sharing of mutable
> > objects is confusing me.
>
> Each class gets its own namespace, but names are different from
> objects.  For example:
>
> >>> x = [1, 2, 3]
> >>> y = x
> >>> y.append(4)
> >>> x
> [1, 2, 3, 4]
>
> In this case, x and y are both different names for the same object.
> Classes increase the name space, but they don't change the fact that
> in python, assignment is just giving something a new name.
>
> --
> John.
>



-- 
John Morris
[EMAIL PROTECTED]
"Do nothing which is of no use." -- Miyamoto Musashi
http://profile.mygamercard.net/nerdality";>
http://card.mygamercard.net/gbar/abyss/nerdality.gif"; border=0>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread Kent Johnson
John Morris wrote:
> Does it have something to do with:
> http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm
> 
> And if so can anyone explain it a bit for me, I'm being slow tonight.
> 
> I thought each class got it's own namespace and this sharing of mutable 
> objects is confusing me.

It has nothing to do with namespaces, it is the semantics of assignment 
that is your problem. What you have done is essentially the same as 
this, just obfuscated ;-)

In [1]: d = { "srv" : "why", "goo" : "sticky" }
In [2]: n = d
In [3]: n.pop('srv')
Out[3]: 'why'
In [4]: d
Out[4]: {'goo': 'sticky'}

Look at the references I sent (or wait for someone else with more energy 
than I to explain :-) )

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Fouhy
On 22/01/2008, John Morris <[EMAIL PROTECTED]> wrote:
> I thought each class got it's own namespace and this sharing of mutable
> objects is confusing me.

Each class gets its own namespace, but names are different from
objects.  For example:

>>> x = [1, 2, 3]
>>> y = x
>>> y.append(4)
>>> x
[1, 2, 3, 4]

In this case, x and y are both different names for the same object.
Classes increase the name space, but they don't change the fact that
in python, assignment is just giving something a new name.

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Morris
Thanks,

so I could/should do

self.ot = Bar(self.name.copy()) instead


On Jan 21, 2008 9:25 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:

> John Morris wrote:
>
> > Why does the pop in the Bar class nuke the srv k & v from Foo.name
> >  as well?
>
> Because they are both names for the same dict.
>
> Assignment in Python does not copy values; it binds a name to a value.
> Some good references:
> http://effbot.org/zone/python-objects.htm
>
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/56e7d62bf66a435c/
>
> Kent
>



-- 
John Morris
[EMAIL PROTECTED]
"Do nothing which is of no use." -- Miyamoto Musashi
http://profile.mygamercard.net/nerdality";>
http://card.mygamercard.net/gbar/abyss/nerdality.gif"; border=0>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Morris
Does it have something to do with:
http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm

And if so can anyone explain it a bit for me, I'm being slow tonight.

I thought each class got it's own namespace and this sharing of mutable
objects is confusing me.

Thanks.


On Jan 21, 2008 9:16 PM, John Morris <[EMAIL PROTECTED]> wrote:

> class Foo:
>   '''Represents a foo'''
>   def __init__(self, name):
> '''Initializes the person's data'''
> self.name = name
> print '(Initializing %s)' % self.name
> self.ot = Bar(self.name)
> print '(After Other - %s)' % self.name
>
> class Bar:
> def __init__(self, name):
> self.name = name
> print 'Other', self.name
> self.name.pop('srv')
> print 'Other (Changed)', self.name
>
> dict = { "srv" : "why", "goo" : "sticky" }
> foo = Foo(dict)
> print foo.name
>
>
> Why does the pop in the Bar class nuke the srv k & v from Foo.name as
> well?
>
>


-- 
John Morris
[EMAIL PROTECTED]
"Do nothing which is of no use." -- Miyamoto Musashi
http://profile.mygamercard.net/nerdality";>
http://card.mygamercard.net/gbar/abyss/nerdality.gif"; border=0>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread Kent Johnson
John Morris wrote:

> Why does the pop in the Bar class nuke the srv k & v from Foo.name 
>  as well?

Because they are both names for the same dict.

Assignment in Python does not copy values; it binds a name to a value. 
Some good references:
http://effbot.org/zone/python-objects.htm
http://groups.google.com/group/comp.lang.python/browse_thread/thread/56e7d62bf66a435c/

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor