Re: [Tutor] Another try at Python's selfishness

2006-02-03 Thread Alan Gauld
> I have to say that as a newbie, it took me quite a while to get my
> head around that extra parameter (self).

That's OK, its true of most folks, even non newbies!
It is one area where Python is different to most languages
who hide the self completely.

> It took me ages to work out that:
>
>class A:
>   def __init__(self, foo):
>   self.foo = foo
>
> a  = A("hello")
>
> is actually a shortcut for:
>
> a = A(a, "Hello")

> I think insisting on:
>
> a = A(a, "Hello")
>
> would be very good for readability, understandability and 
> newbie-friendliness.

Only for the constructor.
Once you have the object itself the method calls wouyld be much less 
readable:

class C:
  def a(s):pass

c = C()
c = c.a(c)
etc

is not descriptive of the concept of passing a message to the object c,
it looks rather like you are passing the object c to a function which is
a member of itself, which brings to mind recursion not messages...

Remember that the purpose of writing a class is to *hide* the details of
its implementation from the user, thus the user never has to use self,
only the builder. The only reason you are confused is because you
are both builder and user of the class.

But consider file objects, do you ever feel the need to write:

myfile = file(myfile,filename,mode)

or does

myfile = file(filename,mode)

seem more friendly? Is that because you've nbever actually
seen the constructor code for the file class(*) and so don't
worry about the presence of self?
If you did have to pass the object in, wouldn't you be asking
why you had to pass the object into the file creation method?

(*)OK I haven't checked,  but I strongly suspect the built in
file objects aren't written in Python so won't look the same
but the illustration is valid.

I dunno if that helps explain the rational but maybe...

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


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


Re: [Tutor] list method help

2006-02-03 Thread Alan Gauld
> > You definitely have to stop thinking of variables as containers. They
> > are pointers or references to values. Another way to think of this is
> > that variables are names for things.
> Kent that's a perfectly understandable and easy to grasp analogy. But it
> stays just an analogy ;-)

Nope. In Python it is what happens.
Python implrements most things using dictionaries under the covers.
When you create a new variable name by doing something like

n = 42

Python does something like:

variables['n'] = 42

where variables is a dictionary.
You can even print the dictionaries if you know the right magic words ;-)

when you access a variable by typing

print n

Python translates that to

print variables['n']

> Thing is.  We people consist of more than a 1st name to identify ourself.

Yes we have identity. And so do Python variables (the actual objects that
the names point at, not the names) and we can print the identity out by
using the id() function:

x = 42
y = x
z = y

print id(x), id(y), id(z)

will all print out the same id since Python caches low value integers
and all three names point at the same stored value.

> coding is not IRL and isn't a reflection of it either.

But creating a reflection of real life is the goal of programming designers
for decades. The whole OOP thing came out of attempte to model
the real world using the simulation language Simula.

> Coding is alot easier bacause it does not require that 2 variables
> need to be linked.

True, but not being able to link them causes its own problems.
Some languages, like C/C++ allow both styles:

int n = 42// n is a memory location which holds 42
int &x = n// n is a memory location that points at whatever n contains.

printf ("%d\t%d",n,x)   / prints 42 twice

n = 27
printf ("%d\t%d",n,x)   / prints 27 twice

Python takes the approach that references are generally easier
to work with than static values, but they do bring their own perils...

> coding with the knowledge A is A and A is never B.

This was true in the very oldest languages like COBOL and
Fortran and BASIC but even Lisp, which is of the same
c1960 vintage, used references. And almoist all languages
since the late 60s have had some form of reference mechanism.

Most languages since the late 1980's have tended to make
variable naming by reference the default.

> When would I have need of this?

When the real world works thatv way, which is often.
When you want to minimise machine resource by avoiding
multiple copies of the same data
When you want to minimise the number of names in your code
- it is essential for dynamic typing!
When you want to use OOP, static objects are virtually
never polymorphic and hence are very limited in usefulness.

> Or why do I want it? Where is the benefit?

The benefits are real and the reasons above indicate some of them.
But there is a dark side as you have discovered. There are times
when a static definition is more logical.

> for x in range(3):
>
> I know it is shorthand for something like (not exactly but it
> serves its purpose as an example):

In fact that may be how you think of it as an analogy to how
you are accustomed to programming but in fact it is nothing
like that. Pythons for loop is much more sophisticated and
uses iterators which in turn use polymorphism within Pythons
object model. That relies on the names being references.

> x = 0
> while x<=3 do:
>x = x +1
>
> And I understand why it is done this way:

So how does your analogy work with this code:

for item in [1,'aString', lambda x: x*x, True]:
print item

You now need to change your code to incorporate
a call to len() and incorporate the concept of an index.
But what if the object being iterated has no len
capability, it may even be infinite in length (a generator
function say)? Like this:

def f(x):
   n = 2*x
   while True:
  n *= 2
  yield n

for y in f(2):
   print y

This is getting into subtleties I know, but it illustrates just
how much more powerful Pythons apparently simple
structures are, and they rely on the use of named
bindings rather than static variable containers.

> Oh and tell me to shut up and just accept it if you want to ;-)

No, its always better to ask. Sometimes we may not understand the answer,
or may not agree with the answer or whatever. But its always better to ask.
And sometimes the answer in Python is simply that Guido van Rossum
prefers it that way!

Alan G.

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


Re: [Tutor] list method help

2006-02-03 Thread Danny Yoo

> > But this assignment sort of puzzles me to why it's done like this
> > (maybe cuz I am not used to it and can not see beyond my own
> > experience in coding (having a blind spot or something like that)).


If we have a snippet of code like:

###
def test():
x = []
f(x)
print x
###

we have a strong guarantee that 'x' won't be redirected to another value.
The situation above, right before calling f(), looks like:

   x --> []

Of course, f() can apply mutation on the list and change the shape of the
list value.  But no matter what, f can't get 'x' directed to an entirely
different list value.


In Python, all names are just arrows to values.  Assignment is the
operation of getting the target on the left hand side to direct its arrow
at the value on the right hand side.  Variable name lookup is just getting
at the value that the arrow is pointing at.

If we're familiar with a language like C, we can imagine that, in Python's
world, all variable names are pointers to things.  So when we see
something like:

##
x = 42
##

if we were to literally translate what Python's does here in a C context,
we'd see something like this:

/**/
int *x = make_int(42);
/**/

where there's a core set of value-making functions available somewhere,
like:

/**/
int* make_int(int val) {
int *result = malloc(sizeof(int));
*result = val;
return result;
/**/


> Good idea. Just get over it, it's really not a big deal. ;-) It's only a
> problem if you try to hold on to the copying model, then you will be
> occasionally surprised.

The copying model works just as well, so long as we keep in mind that
variable names hold arrows (where those arrows aim at values) rather than
the values themselves.


Best of wishes!

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


Re: [Tutor] list method help

2006-02-03 Thread Kent Johnson
Rinzwind wrote:
> On 2/3/06, *Kent Johnson* <[EMAIL PROTECTED] > 
> wrote:
> You definitely have to stop thinking of variables as containers. They
> are pointers or references to values. Another way to think of this is
> that variables are names for things. You may call me Kent, someone else
> might call me Mr. Johnson or Dad, but if I get a haircut, Kent, Mr.
> Johnson and Dad all have shorter hair because all three names refer to
> the same person.
> 
> Python works the same way. Assignment binds a name to a value. So if
> I say
>lst = [0, 1, 2]
> I have bound the name 'lst' to a particular list whose value, at the
> moment, is [0, 1, 2]. If I then assign
>a = lst
> this binds the name 'a' to the same value (the list) that 'lst' is bound
> to. If I change the bound list, you will see the change whether you
> access the list through the name 'a' or the name 'lst'.
> 
> Kent that's a perfectly understandable and easy to grasp analogy. But it 
> stays just an analogy ;-)

The first paragraph above is analogy, the second is commonly used Python 
terminology. For example, from the Language Reference: "Assignment 
statements are used to (re)bind names to values."
http://docs.python.org/dev/ref/assignment.html

> Coding is alot easier bacause it does not require that 2 variables need 
> to be linked. I have been coding with the knowledge A is A and A is 
> never B. Not even if they contain the same content but they can have the 
> content copied from 1 to another :-P
> 
> My question might be summed up to:
> When would I have need of this?  Or why do I want it? Where is the benefit?

You are suggesting that assignment copy values, rather than references 
to values. This could be very expensive. For example if you want to pass 
a large list to a function, passing just a reference to the list is much 
easier than passing a copy of the list.

References are used in other languages, too. With the exception of 
primitive values (int, float, char, etc) Java variables have exactly the 
semantics of Python variables - they are references to objects, and 
assignment copies the reference, not the value. C and C++ can use 
pointers to structures which are similar to Java and Python references. 
So Python is not really so odd, it's just different than what you are 
used to and expect.

> But this assignment sort of puzzles me to why it's done like this (maybe 
> cuz I am not used to it and can not see beyond my own experience in 
> coding (having a blind spot or something like that)).
> 
> Oh and tell me to shut up and just accept it if you want to ;-)

Good idea. Just get over it, it's really not a big deal. ;-) It's only a 
problem if you try to hold on to the copying model, then you will be 
occasionally surprised.

Kent

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


Re: [Tutor] query regarding mysql database backup?? please Help

2006-02-03 Thread Danny Yoo


On Fri, 3 Feb 2006, deepak.gupta wrote:

> I did my script like this for backing my max_everest_2006 mysql database
>
> error: 1044: Access denied for user 'MAX_USER'@'%' to database
> 'MAX_EVEREST_2006' when using LOCK TABLES


Your question doesn't have to do with Python.  Ask your MySQL
administrator to help you: what you're seeing is a MySQL error message.

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


[Tutor] query regarding mysql database backup?? please Help

2006-02-03 Thread deepak.gupta




Hi,
I did my script like this  for backing my 
max_everest_2006 mysql database 
 
>>import os
> > target_dir = 
"./backup"> > os.system("mysqldump --add-drop-table 
-c -u root  -pmysql 'MAX_EVEREST_2006' > 
"+target_dir+"table.bak.sql")
 
 
 root is my user name,mysql is my 
password,'MAX_EVEREST_2006' is my database name. reply me as soon as 
possible
 
error: 1044: Access denied for user 'MAX_USER'@'%' to database 'MAX_EVEREST_2006' 
when using LOCK TABLES
  Please help
            
                
                
                
        Thanks
            
                
                
           
    deepak 
gupta
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list method help

2006-02-03 Thread Rinzwind
On 2/3/06, Kent Johnson <[EMAIL PROTECTED]> wrote:

> On 2/3/06, Chris or Leslie Smith <[EMAIL PROTECTED]> wrote:>>Others could give you a really good answer. I am a BASIC/FORTRAN writer
>>myself, and getting used to the *object* orientation of python took a little
>>while, but after you get the hang of it, it's not bad. In BASIC you think of>>variables *containing* things, so when you say l=2 and a=l you think of two>>variables each containing the (what happens to be) the same thing. In
>>python, however, mutable objects (like lists) are *pointed to* by the>>variable name. so the 'l=range(3)' above tells python to create a list and>>point the variable name 'l' at it. Then when you say 'a=l' you are telling
>>it to point 'a' at the same thing as 'l'--one object (the list); two>>references to it.You definitely have to stop thinking of variables as containers. Theyare pointers or references to values. Another way to think of this is
that variables are names for things. You may call me Kent, someone elsemight call me Mr. Johnson or Dad, but if I get a haircut, Kent, Mr.Johnson and Dad all have shorter hair because all three names refer to

the same person.Python works the same way. Assignment binds a name to a value. So if I say   lst = [0, 1, 2]I have bound the name 'lst' to a particular list whose value, at themoment, is [0, 1, 2]. If I then assign
   a = lstthis binds the name 'a' to the same value (the list) that 'lst' is boundto. If I change the bound list, you will see the change whether youaccess the list through the name 'a' or the name 'lst'.

KentKent that's a perfectly understandable and easy to grasp analogy. But it stays just an analogy ;-)Thing is.  We people consist of more than a 1st name to identify ourself. It is 1st name, last name, country, place of birth, town you now live in, street, postal code, number of the house, date of birth and in case of twins or triplets even the time of birth and maybe a bunch more details.  But coding is not IRL and isn't a reflection of it either. Coding is alot easier bacause it does not require that 2 variables need to be linked. I have been coding with the knowledge A is A and A is never B. Not even if they contain the same content but they can have the content copied from 1 to another :-P
My question might be summed up to:When would I have need of this?  Or why do I want it? Where is the benefit?When I look at this:for x in range(3):I know it is shorthand for something like (not exactly but it serves its purpose as an example):
x = 0while x<=3 do:   x = x +1And I understand why it is done this way: it makes code shorter AND it is even easier to read: a win-win situation. And the same goes for alot of other things in Python.
But this assignment sort of puzzles me to why it's done like this (maybe cuz I am not used to it and can not see beyond my own experience in coding (having a blind spot or something like that)).Oh and tell me to shut up and just accept it if you want to ;-)


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


Re: [Tutor] subclass

2006-02-03 Thread Kent Johnson
Christopher Spears wrote:
> Here is a problem I'm working on out of Learning
> Python:
> 
> Make a subclass of MyList from exercise 2 called
> MyListSub which extends MyList to print a message to
> stdout before each overloaded operation is called and
> counts the number of calls.  MyListSub should inherit
> basic method behavoir from MyList.  Adding a sequence
> to a MyListSub should print a message, increment the
> counter for + calls, and perform the superclass's
> method.
> > 
> However, I keep wondering if there is a more elegant
> way to do this like create some sort of function that
> does this:
> 
> if a method is called:
> print a message
> 
> if a method is called:
> increment a counter for that method

You could do this by overriding __getattribute__ in MyListSub. This 
would allow you to put all the printing and counting code in one place. 
This is an elegant solution that will probably cause you a couple of 
stack overflows before you get it right.
http://docs.python.org/ref/new-style-attribute-access.html

You could also write a decorator that adds printing and counting 
behaviour to a method. You would have to apply the decorator to each 
method. I suppose you could apply the decorator in a metaclass...but I 
would try the __getattribute__ approach first.

Neither of these approaches are really beginner exercises...
Kent


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


[Tutor] subclass

2006-02-03 Thread Christopher Spears
Here is a problem I'm working on out of Learning
Python:

Make a subclass of MyList from exercise 2 called
MyListSub which extends MyList to print a message to
stdout before each overloaded operation is called and
counts the number of calls.  MyListSub should inherit
basic method behavoir from MyList.  Adding a sequence
to a MyListSub should print a message, increment the
counter for + calls, and perform the superclass's
method.

There is more after that, but I will tackle that after
I clear up this part.  Here is MyList.py:

class MyList:
def __init__(self, start):
self.mylist = []
for x in start: self.mylist.append(x)
def __getitem__(self, index):
return self.mylist[index]
def __setitem__(self, index, value):
self.mylist[index] = value
def __len__(self):
return len(self.mylist)
def __delitem__(self, index):
del self.mylist[index]
def __add__(self, other):
return MyList(self.mylist + other)
def __mul__(self, other):
return MyList(self.mylist * other)
def __getslice__(self, low, high):
return MyList(self.mylist[low:high])
def __repr__(self):
return '%s' % self.mylist
def append(self, other):
self.mylist.append(other)
def count(self, value):
return self.mylist.count(value)
def index(self, value):
return self.mylist.index(value)
def extend(self, seq):
self.mylist.extend(seq)
def insert(self, index, value):
self.mylist.insert(index, value)
def pop(self, index):
return self.mylist.pop(index)
def remove(self, value):
self.mylist.remove(value)
def reverse(self):
self.mylist.reverse()
def sort(self):
self.mylist.sort()

My first thought for printing messages was to do
something like this:

MyListSub.py:

from MyList import *

class MyListSub(MyList):
def __init__(self, start):
print "implementing __init__ method for MyListSub"
self.mylist = []
for x in start: self.mylist.append(x)

This is what I call the idiot's method.  Basically, I
would put a print message in each method as well as
some sort of counter.

However, I keep wondering if there is a more elegant
way to do this like create some sort of function that
does this:

if a method is called:
print a message

if a method is called:
increment a counter for that method
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list method help

2006-02-03 Thread Kent Johnson
> On 2/3/06, Chris or Leslie Smith <[EMAIL PROTECTED]> wrote:
>>Others could give you a really good answer. I am a BASIC/FORTRAN writer
>>myself, and getting used to the *object* orientation of python took a little
>>while, but after you get the hang of it, it's not bad. In BASIC you think of
>>variables *containing* things, so when you say l=2 and a=l you think of two
>>variables each containing the (what happens to be) the same thing. In
>>python, however, mutable objects (like lists) are *pointed to* by the
>>variable name. so the 'l=range(3)' above tells python to create a list and
>>point the variable name 'l' at it. Then when you say 'a=l' you are telling
>>it to point 'a' at the same thing as 'l'--one object (the list); two
>>references to it.

You definitely have to stop thinking of variables as containers. They 
are pointers or references to values. Another way to think of this is 
that variables are names for things. You may call me Kent, someone else 
might call me Mr. Johnson or Dad, but if I get a haircut, Kent, Mr. 
Johnson and Dad all have shorter hair because all three names refer to 
the same person.

Python works the same way. Assignment binds a name to a value. So if I say
   lst = [0, 1, 2]
I have bound the name 'lst' to a particular list whose value, at the 
moment, is [0, 1, 2]. If I then assign
   a = lst
this binds the name 'a' to the same value (the list) that 'lst' is bound 
to. If I change the bound list, you will see the change whether you 
access the list through the name 'a' or the name 'lst'.

Kent

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


Re: [Tutor] list method help

2006-02-03 Thread Rinzwind
On 2/3/06, Chris or Leslie Smith <[EMAIL PROTECTED]> wrote:
> Rinzwind wrote:
> | Well Chris or Leslie Smith.
> |
> | This bit l[:]=l[-1:]+l[0:-1] I think is VERY elegant. When I saw this
> | in your post I tought: DUH.
> | I did the same with 1 line more but I am still new to python ;)
> |
> You're seeing the beauty of the language at work :-)

I sure do :)
I also love dictionaries, tuples, and lists. Thoroughbred Basic
doesn't work like that :*

> | Regarding the rest of the 'aside'.
> |
> | What is the reasoning behind this:
> |
> | ###
>  l=range(3)
>  a=l # 'a' is pointing at what 'l' is pointing at
>  l[0]=42 # I make a change to the thing that 'l' points to
>  print a
> | [42, 1, 2]
>  print l
> | [42, 1, 2]
> 
> | ###
> |
> | -Why- are these 2 bound together? Is there a specific reasoning
> | behind it?
> |
> | Cuz in Basic
>
> Others could give you a really good answer. I am a BASIC/FORTRAN writer
> myself, and getting used to the *object* orientation of python took a little
> while, but after you get the hang of it, it's not bad. In BASIC you think of

10 FOR P1= 1 TO 1; PRINT 'Hello Chris'; NEXT P1

That Basic not VB ;-)

> variables *containing* things, so when you say l=2 and a=l you think of two
> variables each containing the (what happens to be) the same thing. In
> python, however, mutable objects (like lists) are *pointed to* by the
> variable name. so the 'l=range(3)' above tells python to create a list and
> point the variable name 'l' at it. Then when you say 'a=l' you are telling
> it to point 'a' at the same thing as 'l'--one object (the list); two
> references to it.

Yes, I read something about it concerning ID's assigned to variables.
So A = [1,2,3,4] has an ID 209370
And L =  A gives L the ID 209370 matching them together.

It's cool :-) and would save me alot of  A=L stmts in Basic.
'-)

<..>
> | What is the reasoning behind this:
> |
> | ###
>  l=range(3)
>  a=l # 'a' is pointing at what 'l' is pointing at
>  l[0]=42 # I make a change to the thing that 'l' points to
>  a=l  <--- this one added by ME.
>  print a
> | [42, 1, 2]
>  print l
> | [42, 1, 2]
> 
> | ###
>
> I know what you are getting at, but from what I wrote above do you see the
> difference yet? In basic, saying 'a=l' has created a new variable/value item
> and if you want the new change to be reflected in a after making a change to
> l you have to 'copy it there' with another a=l statement. In python there is
> only one object (the [0, 1, 2]) with two variables pointing to it (a and l).
> Those pointer-varaibles are like aliases for the same thing.

Oh I understand it :) Was just wondering -why-. I sometimes am like a
10 year old wanting not to know how it works by why it was created to
work that way. There must be a reason ;-)

<..>
>
> p.s. I am Chris. My wife and I share the same account, so that's why there
> is a "or" in the email name :-)
>
>

Well I can't guess if it's you, your wife or you are both behind your
PC beating eachother over the head to decide who can answer my
questions :-D
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: list method help

2006-02-03 Thread Rinzwind
Chris or Leslie Smith: sorry wrong addess :*(


This bit l[:]=l[-1:]+l[0:-1] I think is VERY elegant. When I saw this
in your post I tought: DUH.
I did the same with 1 line more but I am still new to python ;)

Regarding the rest of the 'aside'.

What is the reasoning behind this:

###
>>> l=range(3)
>>> a=l # 'a' is pointing at what 'l' is pointing at
>>> l[0]=42 # I make a change to the thing that 'l' points to
>>> print a
[42, 1, 2]
>>> print l
[42, 1, 2]
>>>
###

-Why- are these 2 bound together? Is there a specific reasoning behind it?

Cuz in Basic (if we had lists) I'd expect this:

###
>>> l=range(3)
>>> a=l # 'a' is pointing at what 'l' is pointing at
>>> l[0]=42 # I make a change to the thing that 'l' points to
>>> print a
[0, 1, 2]
>>> print l
[42, 1, 2]
>>>
###

To get there you'd need 1 more line:

What is the reasoning behind this:

###
>>> l=range(3)
>>> a=l # 'a' is pointing at what 'l' is pointing at
>>> l[0]=42 # I make a change to the thing that 'l' points to
>>> a=l  <--- this one added by ME.
>>> print a
[42, 1, 2]
>>> print l
[42, 1, 2]
>>>
###


Oh and I DO think it's a cool feature btw. But I had me guessing alot too.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Another try at Python's selfishness

2006-02-03 Thread Ed Singleton
On 3 Feb 2006 03:59:10 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> > I still see "newbie-friendliness" as a
> > MAJOR plus for Python -- it increases the chance that users
> > of your software will become contributors.
>
> Yes, I 100% agree to that point!
> But the point is, the current situation is not newbie-friendly (I can
> tell, I am a newbie): I declare a method with 3 parameters but when I
> call it I only pass 2 parameters. That's confusing. If I declare a
> member variable, I write: "self.x  = ValueForX", why can't I be equally
> explicit for declaring member functions?

I have to say that as a newbie, it took me quite a while to get my
head around that extra parameter (self).

It took me ages to work out that:

class A:
   def __init__(self, foo):
   self.foo = foo

a = A("hello")

is actually a shortcut for:

a = A(a, "Hello")

or even:

a = new A(a, "Hello")

I think insisting on:


a = A(a, "Hello")

would be very good for readability, understandability and newbie-friendliness.

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