Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Why no lexical scoping for a method within a class? (http://www.velocityreviews.com/forums/t649046-why-no-lexical-scoping-for-a-method-within-a-class.html)

walterbyrd 12-17-2008 03:19 PM

Why no lexical scoping for a method within a class?
 
For a language as well structured as Python, this seems somewhat
sloppy, and inconsistant. Or is there some good reason for this?

Here is what I mean:

def a():
x = 99
print x

def b():
print x

a()
b() # raises an exception because x is not defined.

However in the methods are within a class, the scoping seems to work
differently.

class ab():
def a(self):
self.x = 99
print self.x
def b(self):
print self.x

i = ab()
i.a()
i.b() # this works, why no lexical scoping?

Diez B. Roggisch 12-17-2008 03:41 PM

Re: Why no lexical scoping for a method within a class?
 
walterbyrd wrote:

> For a language as well structured as Python, this seems somewhat
> sloppy, and inconsistant. Or is there some good reason for this?
>
> Here is what I mean:
>
> def a():
> x = 99
> print x
>
> def b():
> print x
>
> a()
> b() # raises an exception because x is not defined.
>
> However in the methods are within a class, the scoping seems to work
> differently.
>
> class ab():
> def a(self):
> self.x = 99
> print self.x
> def b(self):
> print self.x
>
> i = ab()
> i.a()
> i.b() # this works, why no lexical scoping?


Because what you do is to create instance variables. Why do you expect them
not working if you explicitly access them?

The real analog of your example would be this:

class ab():
def a(self):
x = 100
print x

def b(self):
print x

which provokes the same error.

however, there *are* different scoping rules, classes don't create a lexical
scope for their own variables:


class foo(object):
x = 100

def a(self):
print x


Diez

pruebauno@latinmail.com 12-17-2008 03:41 PM

Re: Why no lexical scoping for a method within a class?
 
On Dec 17, 10:19*am, walterbyrd <walterb...@iname.com> wrote:
> For a language as well structured as Python, this seems somewhat
> sloppy, and inconsistant. *Or is there some good reason for this?
>
> Here is what I mean:
>
> def a():
> * * x = 99
> * * print x
>
> def b():
> * * print x
>
> a()
> b() # raises an exception because x is not defined.
>
> However in the methods are within a class, the scoping seems to work
> differently.
>
> class ab():
> * * def a(self):
> * * * * self.x = 99
> * * * * print self.x
> * * def b(self):
> * * * * print self.x
>
> i = ab()
> i.a()
> i.b() # this works, why no lexical scoping?


If scoping worked as you want, how, pray tell, would you define object
attributes?

John Machin 12-17-2008 03:54 PM

Re: Why no lexical scoping for a method within a class?
 
On Dec 18, 2:19*am, walterbyrd <walterb...@iname.com> wrote:
> For a language as well structured as Python, this seems somewhat
> sloppy, and inconsistant. *Or is there some good reason for this?
>
> Here is what I mean:
>
> def a():
> * * x = 99
> * * print x
>
> def b():
> * * print x
>
> a()
> b() # raises an exception because x is not defined.
>
> However in the methods are within a class, the scoping seems to work
> differently.
>
> class ab():
> * * def a(self):
> * * * * self.x = 99
> * * * * print self.x
> * * def b(self):
> * * * * print self.x
>
> i = ab()
> i.a()
> i.b() # this works, why no lexical scoping?


Two questions for you:
(1) If you were to change the sloppy inconsistent scoping mechanism in
classes, what would you change it to?
(2) What do you think of the following:
class ab():
def a(self):
self.x = 99
print self.x
def b(me):
print me.x
?

rdmurray@bitdance.com 12-17-2008 04:04 PM

Why no lexical scoping for a method within a class?
 
Quoth walterbyrd <walterbyrd@iname.com>:
> For a language as well structured as Python, this seems somewhat
> sloppy, and inconsistant. Or is there some good reason for this?


Yes. It's called Object Oriented Programming.

> Here is what I mean:
>
> def a():
> x = 99
> print x
>
> def b():
> print x
>
> a()
> b() # raises an exception because x is not defined.
>
> However in the methods are within a class, the scoping seems to work
> differently.
>
> class ab():
> def a(self):
> self.x = 99
> print self.x
> def b(self):
> print self.x
>
> i = ab()
> i.a()
> i.b() # this works, why no lexical scoping?


Because x is an attribute.

If you don't understand what that means, read any introductory
article on OOP.

To give you a clue, if you had said:

class ab():
def a(self):
x = 99
print x
def b(self):
print x

You'd have gotten the exception you expected (assuming x wasn't
defined globally).

--RDM


r 12-17-2008 05:00 PM

Re: Why no lexical scoping for a method within a class?
 
I think there is a simpler answer to all this(not to take away from
the other ones here though which are all great). When writing
procedural code how would you like it if vars inside functions were
automatically global. Your code with be blowing chunks in no time.
Thats the reason for global declarations and instance.var :)



Richard Brodie 12-17-2008 05:17 PM

Re: Why no lexical scoping for a method within a class?
 

"walterbyrd" <walterbyrd@iname.com> wrote in message
news:518b9dd9-69c5-4d5b-bd5f-ad567be6249b@b38g2000prf.googlegroups.com...

> However in the methods are within a class, the scoping seems to work
> differently.


Not really, self is a formal parameter to the function. It would be
a strange language where a function's own arguments weren't in scope.

> def b(self):
> print self.x


Try changing it to:

def b(somethingotherthanself):
print self.x




walterbyrd 12-17-2008 06:20 PM

Re: Why no lexical scoping for a method within a class?
 
On Dec 17, 10:00*am, r <rt8...@gmail.com> wrote:
> When writing
> procedural code how would you like it if vars inside functions were
> automatically global. Your code with be blowing chunks in no time.


That was my point - I consider python's ordinary use of lexical
scoping to be a good thing, and I was wondering why this "good thing"
was not used in classes, as well as outside of classes.


walterbyrd 12-17-2008 06:27 PM

Re: Why no lexical scoping for a method within a class?
 
On Dec 17, 8:41*am, prueba...@latinmail.com wrote:

> If scoping worked as you want, how, pray tell, would you define object
> attributes?- Hide quoted text -


I suppose you could do this:

class className():
varname = "whatever"
def fname(self, varname):
. . . .

Instead of having variable defined within methods to be global
everywhere within the class.

Anyway, it's not a matter of what I like, I am just trying to
understand the reason behind the scoping rules.


walterbyrd 12-17-2008 06:30 PM

Re: Why no lexical scoping for a method within a class?
 
On Dec 17, 9:04*am, rdmur...@bitdance.com wrote:

> Yes. *It's called Object Oriented Programming.


I think you mean it's *Python* Object Oriented Programming. I am not
sure that every other OO language works like that.


All times are GMT. The time now is 07:28 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.