Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why no lexical scoping for a method within a class?

Reply
Thread Tools

Why no lexical scoping for a method within a class?

 
 
walterbyrd
Guest
Posts: n/a
 
      12-17-2008
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?
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      12-17-2008
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
 
Reply With Quote
 
 
 
 
pruebauno@latinmail.com
Guest
Posts: n/a
 
      12-17-2008
On Dec 17, 10:19*am, walterbyrd <(E-Mail Removed)> 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?
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-17-2008
On Dec 18, 2:19*am, walterbyrd <(E-Mail Removed)> 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
?
 
Reply With Quote
 
rdmurray@bitdance.com
Guest
Posts: n/a
 
      12-17-2008
Quoth walterbyrd <(E-Mail Removed)>:
> 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

 
Reply With Quote
 
r
Guest
Posts: n/a
 
      12-17-2008
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


 
Reply With Quote
 
Richard Brodie
Guest
Posts: n/a
 
      12-17-2008

"walterbyrd" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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



 
Reply With Quote
 
walterbyrd
Guest
Posts: n/a
 
      12-17-2008
On Dec 17, 10:00*am, r <(E-Mail Removed)> 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.

 
Reply With Quote
 
walterbyrd
Guest
Posts: n/a
 
      12-17-2008
On Dec 17, 8:41*am, (E-Mail Removed) 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.

 
Reply With Quote
 
walterbyrd
Guest
Posts: n/a
 
      12-17-2008
On Dec 17, 9:04*am, (E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C closures & lexical scoping Khookie C Programming 28 12-15-2007 10:50 PM
python newbie - question about lexical scoping Matt Barnicle Python 10 12-02-2007 08:59 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Lexical scoping question. Louis. Perl Misc 8 02-11-2005 03:45 AM
(?{..}) and lexical scoping issues. Aronaxis, the Sourceror Perl Misc 3 06-21-2004 10:04 PM



Advertisments