[Tutor] Proper uses of classes

2013-04-03 Thread frank ernest
Hi guys, it's been a while since I posted and I've learned a lot since then. 
Today I have a question on classes, I can't get mine to work.
class alist(list):
 def __init__(self, b, a):
 self = list()
 self.append(b)
 a = a + b
 def appendit(self):
 self.append(a)
 
print(alist(2,4))
[]
#It's blank!
c = alist(2,4)
c.appendit()
print(c)
[[...]]
#It's still blank!
If I add this:
 a = a + b
 
the last line of my deffinition I get:
c = alist(2,4)
c.appendit()
Traceback (most recent call last):
File "", line 1, in 
File "", line 7, in appendit
UnboundLocalError: local variable 'a' referenced before assignment
If I make a nonlocal I get
SyntaxError: name 'a' is parameter and nonlocal
I want it to get my list and all the members in it when printing for instance. 
I also would like to without making them global create two variables which I 
can use throughout the whole class as their value will not change.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Proper uses of classes

2013-04-03 Thread Dave Angel

On 04/03/2013 03:43 PM, frank ernest wrote:

Hi guys, it's been a while since I posted and I've learned a lot since then. 
Today I have a question on classes, I can't get mine to work.


Welcome back.  I'll try.  First comment is that indentation ought to be 
4 columns, until you have enough experience to prefer something 
different.  Certainly one column makes the code nearly illegible to me.


Second comment is that deriving from a built-in class like list is 
tricky.  It won't be enough to define an __init__() method, and it turns 
out you're not using the inheritance anyway.  So please get this working 
using only object as your base.  You're probably using Python3.x, where 
object is implied, but it doesn't hurt to be specific.


class alist(object):


class alist(list):
  def __init__(self, b, a):
  self = list()


You forgot to indent the body of the method.  That's an IndentationError 
during compile, so I suspect this code is NOT what you tried.  I'll 
assume you indent the lines from here through the 'def appendit' line.


Please use copy/paste to put your code into your email message.  If you 
retype it, you waste everybody's time.


Don't assign to 'self,' as once you do, you no longer have any access to 
the class instance.  It's a parameter, automatically passed to your 
method.  You probably want to add an attribute to hold the new list.


I'll assume you change it to:
self.mylist = list()
self.mylist.append(b)


  self.append(b)



  a = a + b
  def appendit(self):
  self.append(a)


You didn't indent again.  Further, you referenced a, which was a 
parameter to an entirely different method.  If you meant for it to be 
saved, you could have made it a new attribute in the first method, by using

self.a = a


 
print(alist(2,4))


You never added anything to the list, so of course it prints out as 
empty.  You created a new one inside your class.



[]
#It's blank!
c = alist(2,4)
c.appendit()
print(c)
[[...]]
#It's still blank!
If I add this:
  a = a + b
 
the last line of my deffinition I get:
c = alist(2,4)
c.appendit()
Traceback (most recent call last):
File "", line 1, in 
File "", line 7, in appendit
UnboundLocalError: local variable 'a' referenced before assignment
If I make a nonlocal I get
SyntaxError: name 'a' is parameter and nonlocal
I want it to get my list and all the members in it when printing for instance. 
I also would like to without making them global create two variables which I 
can use throughout the whole class as their value will not change.



If you want a custom printing behavior, you'd need to define a __str__() 
method in your class.  Since you don't, and since you inherit from list, 
you get its behavior.


I made the assumption throughout that it was a mistake to inherit from 
list.  Most of my answers could have been different if you were really 
deliberate about inheriting from list.  For example, you wouldn't be 
able to create your own list, but would have to use the one that you 
inherited from.  You'd have to define a __new__() method, and call 
list's __new__() method from it.   And if you created extra instances as 
your last sentence says, you'd have to define your own __str__() method, 
as the built in list logic wouldn't recognize anything you added.


In any case, I'd write a few successful plain classes before even 
beginning to do inheritance.


HTH

--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Proper uses of classes

2013-04-03 Thread Alan Gauld

On 03/04/13 20:43, frank ernest wrote:

Hi guys, it's been a while since I posted and I've learned a lot since
then. Today I have a question on classes, I can't get mine to work.


Dave has already addressed many issues but there are a few more to consider


class alist(list):

 def __init__(self, b, a):
 self = list()


This should be indented but it makes no sense. You are trying to create 
a subclass of list but by assigning self to list you no longer have a 
reference to your class, self now points at an instance of the builtin 
list. self is already a list by the time you get to __init__.



 self.append(b)


This could work without the list() assignment above.


 a = a + b


this makes no sense since a and b are arguments passed into the method 
and you don't store the result so it gets thrown away, I assume you intended


self.a = a+b


 def appendit(self):
 self.append(a)


Again I assume you meant

self.append(self.a)

And this is odd indeed since it can only ever keep adding the 'a' 
specified during initialization. Normally I'd expect an item to be 
appended to be included in the method call? But then you already have 
that with the inherited append method son maybe this odd behaviour is 
what distinguishes your list from the built in list?



print(alist(2,4))

[]

#It's blank!


Yes because you changed self to be a list and so your alist object had 
no changes made to it.



c = alist(2,4)
c.appendit()
print(c)
[[...]]

#It's still blank!


No, it now has a list inside, although how that got there I'm not sure!



If I add this:
 a = a + b
the last line of my deffinition I get:


Again you omit self so you are only redefining the local variables, 
except there is no 'a' or 'b' to refer to.




c = alist(2,4)

c.appendit()

Traceback (most recent call last):
File "", line 1, in 
File "", line 7, in appendit
UnboundLocalError: local variable 'a' referenced before assignment


Since you didn't pass an a into the method you can't assign it to a.


If I make a nonlocal I get


What do you mean by making it non local?
What did you do?


SyntaxError: name 'a' is parameter and nonlocal

I want it to get my list and all the members in it when printing for
instance. I also would like to without making them global create two
variables which I can use throughout the whole class as their value will
not change.


Your problems lie in how you build the class in init...
Get the initialisation right and the append/print methods
will be easy. But see Dave's comments re __str__...

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Classes in multiple files

2013-04-03 Thread Phil

Thank you for reading this.

The recent question relating to classes has prompted to ask this one.

I have a main window class and a dialog class and they are defined in 
separate files. I'm attempting to display the dialog when a menu item is 
selected in the main window but it doesn't work because, I think, the 
main window class isn't aware of the dialog class in another file.


This is the beginning of the main window class;

class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent = None):

And this is the beginning of the dialog class;

class SatelliteListDialog(QDialog, Ui_Dialog):
def __init__(self, parent = None):

If this was C++ then I'd need to include a class header and so I'm 
thinking that an import statement is needed in the main window class 
referring to the dialog class. Import SatelliteListDialog isn't correct.


There are many on-line examples that deal with displaying dialogs, 
however, both classes are in the same file rather that being separated.


No doubt, this is a trivial question but, as yet, I have not found an 
answer.


--
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Classes in multiple files

2013-04-03 Thread Alan Gauld

On 04/04/13 00:14, Phil wrote:


If this was C++ then I'd need to include a class header and so I'm
thinking that an import statement is needed in the main window class
referring to the dialog class. Import SatelliteListDialog isn't correct.


Reme,ber that in python its the file name you need to use to import

So if SatelliteListDialog is defined in satellite.py you need

import satellite

in your main window file.
And satellite.py has to be in your library path too...

Then in your main window class you need to access the dialog with

   self.sld = satellite.SatelliteListDialog(...)

or whatever. In other words you need the module name prefix.

If that still doesn't work post some code and any error messages.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Doubt in for loop

2013-04-03 Thread bessenkphilip

Hi all,

I'm having a doubt in the below program's 2n'd "for" loop.

>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

My doubt is that "will 'x' be always of value 2, if so why that for loop 
"for x in range(2, n):"
i don't know how the first output , as If 2%2==0:(this satisfies the if 
loop as x =2) , so how the else part came to output i.e 2 is a prime number.


Please educate me in this.

Thanks in advance,
 -Bessen
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Classes in multiple files

2013-04-03 Thread Phil

On 04/04/13 09:32, Alan Gauld wrote:

On 04/04/13 00:14, Phil wrote:


If this was C++ then I'd need to include a class header and so I'm
thinking that an import statement is needed in the main window class
referring to the dialog class. Import SatelliteListDialog isn't correct.


Reme,ber that in python its the file name you need to use to import

So if SatelliteListDialog is defined in satellite.py you need

import satellite

in your main window file.
And satellite.py has to be in your library path too...



That makes sense, much like a header file in C++. I haven't set a 
library path, so I'm using the default, what ever it may be. I'll 
investigate this.



Then in your main window class you need to access the dialog with

self.sld = satellite.SatelliteListDialog(...)

or whatever. In other words you need the module name prefix.

If that still doesn't work post some code and any error messages.



At the moment I'm a little uncertain how I should proceed from here but 
I'll give it a go.


--
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Classes in multiple files - code included

2013-04-03 Thread Phil

On 04/04/13 09:32, Alan Gauld wrote:



Reme,ber that in python its the file name you need to use to import

So if SatelliteListDialog is defined in satellite.py you need

import satellite

in your main window file.
And satellite.py has to be in your library path too...


The Eric IDE must take care of the path. All I had to do was import the 
file containing the dialog class.



Then in your main window class you need to access the dialog with

self.sld = satellite.SatelliteListDialog(...)

or whatever. In other words you need the module name prefix.

If that still doesn't work post some code and any error messages.


I think I must be very close now so I'll post just the code that I think 
is relevant.


This the main window class:

from PyQt4.QtGui import QMainWindow
from PyQt4.QtCore import pyqtSignature

from Ui_mainwindow import Ui_MainWindow

from PyQt4 import QtGui

import satListDialog

class MainWindow(QMainWindow, Ui_MainWindow):

This is the function to show the dialog;

def on_actionList_triggered(self):

self.dialog = Ui_satListDialog.SatelliteListDialog()
self.dialog.show()

Finally, this is the dialog class;

from PyQt4.QtGui import QDialog
from PyQt4.QtCore import pyqtSignature

from Ui_satListDialog import Ui_Dialog

class SatelliteListDialog(QDialog, Ui_Dialog):

And this is the error message;

"global name 'Ui_satListDialog' is not defined"

I have tried just about every conceivable combination in the 
on_actionList_triggered(self) function but I still end up with a "not 
defined" error.


--
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Doubt in for loop

2013-04-03 Thread Steven D'Aprano

On 04/04/13 12:29, bessenkphilip wrote:

Hi all,

I'm having a doubt in the below program's 2n'd "for" loop.


for n in range(2, 10):

... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

My doubt is that "will 'x' be always of value 2, if so why that for loop "for x in 
range(2, n):"
i don't know how the first output , as If 2%2==0:(this satisfies the if loop as 
x =2) , so how the else part came to output i.e 2 is a prime number.


I'm sorry, I don't understand your question.

x is *not* always of value 2. You can see with the last line,

9 equals 3 * 3

x has value 3.


The outer loop just checks 2, 3, 4, ... 9 to see whether they are prime. The 
inner loop actually does the checking:


for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break


This tests whether n is divisible by 2, 3, 4, 5, 6, ... up to n-1. If n is 
divisible by any of those numbers, then n cannot be prime.

For example, with n = 9, the inner loop does this:

x = 2
Test if 2 is a factor: does 9/2 have remainder zero? No.
x = 3
Test if 3 is a factor: does 9/3 have remainder zero? Yes.
So 9 is not prime, and 9 = 3 * (9/3) = 3 * 3


If we test it with n = 35, the inner loop would do this:

x = 2
Test if 2 is a factor: does 35/2 have remainder zero? No.
x = 3
Test if 3 is a factor: does 35/3 have remainder zero? No.
x = 4
Test if 4 is a factor: does 35/4 have remainder zero? No.
x = 5
Test if 5 is a factor: does 35/5 have remainder zero? Yes.
So 35 is not prime, and 35 = 5 * (35/5) = 5 * 7



Notice that this does more work than necessary! Can you see what work it does 
that is unnecessary?

(Hint: what even numbers are prime?)




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Classes in multiple files - code included

2013-04-03 Thread Steven D'Aprano

On 04/04/13 12:47, Phil wrote:


And this is the error message;

"global name 'Ui_satListDialog' is not defined"


On its own, that is almost useless.

Python gives you more debugging information than that: it gives you a complete 
traceback, which includes the actual line of code causing the problem. We don't 
even know which file gives the error, let alone which line of code!

When you try to run the program, you will get an error. Please copy and paste the 
*complete* traceback, starting with the line "Traceback (most recent call 
last)" all the way to the bottom. It will show you which file contains the error, 
what type of error it is, and which line fails.


Thank you.




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Doubt in for loop

2013-04-03 Thread Dave Angel

On 04/03/2013 09:53 PM, Steven D'Aprano wrote:

On 04/04/13 12:29, bessenkphilip wrote:

Hi all,

I'm having a doubt in the below program's 2n'd "for" loop.


for n in range(2, 10):

... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

My doubt is that "will 'x' be always of value 2, if so why that for
loop "for x in range(2, n):"
i don't know how the first output , as If 2%2==0:(this satisfies the
if loop as x =2) , so how the else part came to output i.e 2 is a
prime number.


I'm sorry, I don't understand your question.

x is *not* always of value 2. You can see with the last line,

9 equals 3 * 3

x has value 3.


The outer loop just checks 2, 3, 4, ... 9 to see whether they are prime.
The inner loop actually does the checking:


for x in range(2, n):
 if n % x == 0:
 print n, 'equals', x, '*', n/x
 break


This tests whether n is divisible by 2, 3, 4, 5, 6, ... up to n-1. If n
is divisible by any of those numbers, then n cannot be prime.

For example, with n = 9, the inner loop does this:

x = 2
Test if 2 is a factor: does 9/2 have remainder zero? No.
x = 3
Test if 3 is a factor: does 9/3 have remainder zero? Yes.
So 9 is not prime, and 9 = 3 * (9/3) = 3 * 3


If we test it with n = 35, the inner loop would do this:

x = 2
Test if 2 is a factor: does 35/2 have remainder zero? No.
x = 3
Test if 3 is a factor: does 35/3 have remainder zero? No.
x = 4
Test if 4 is a factor: does 35/4 have remainder zero? No.
x = 5
Test if 5 is a factor: does 35/5 have remainder zero? Yes.
So 35 is not prime, and 35 = 5 * (35/5) = 5 * 7



Notice that this does more work than necessary! Can you see what work it
does that is unnecessary?

(Hint: what even numbers are prime?)






I don't understand the questions either, but I can point out one thing 
that might be puzzling the OP:


When n is 2, the inner loop does nothing, it just skips to the else 
clause.  The reason is that range(2,2) is a null iterator.  range(i,j) 
produces values from i to j-1, or to put it another way values for which

   i <= n < j

If i and j are identical, there's nothing to match it.


--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Doubt in for loop

2013-04-03 Thread antipode0
I believe Dave answered the OP's intended question.
On Apr 3, 2013 9:54 PM, "Dave Angel"  wrote:

> On 04/03/2013 09:53 PM, Steven D'Aprano wrote:
>
>> On 04/04/13 12:29, bessenkphilip wrote:
>>
>>> Hi all,
>>>
>>> I'm having a doubt in the below program's 2n'd "for" loop.
>>>
>>>  for n in range(2, 10):
>>
> ... for x in range(2, n):
>>> ... if n % x == 0:
>>> ... print n, 'equals', x, '*', n/x
>>> ... break
>>> ... else:
>>> ... # loop fell through without finding a factor
>>> ... print n, 'is a prime number'
>>> ...
>>> 2 is a prime number
>>> 3 is a prime number
>>> 4 equals 2 * 2
>>> 5 is a prime number
>>> 6 equals 2 * 3
>>> 7 is a prime number
>>> 8 equals 2 * 4
>>> 9 equals 3 * 3
>>>
>>> My doubt is that "will 'x' be always of value 2, if so why that for
>>> loop "for x in range(2, n):"
>>> i don't know how the first output , as If 2%2==0:(this satisfies the
>>> if loop as x =2) , so how the else part came to output i.e 2 is a
>>> prime number.
>>>
>>
>> I'm sorry, I don't understand your question.
>>
>> x is *not* always of value 2. You can see with the last line,
>>
>> 9 equals 3 * 3
>>
>> x has value 3.
>>
>>
>> The outer loop just checks 2, 3, 4, ... 9 to see whether they are prime.
>> The inner loop actually does the checking:
>>
>>
>> for x in range(2, n):
>>  if n % x == 0:
>>  print n, 'equals', x, '*', n/x
>>  break
>>
>>
>> This tests whether n is divisible by 2, 3, 4, 5, 6, ... up to n-1. If n
>> is divisible by any of those numbers, then n cannot be prime.
>>
>> For example, with n = 9, the inner loop does this:
>>
>> x = 2
>> Test if 2 is a factor: does 9/2 have remainder zero? No.
>> x = 3
>> Test if 3 is a factor: does 9/3 have remainder zero? Yes.
>> So 9 is not prime, and 9 = 3 * (9/3) = 3 * 3
>>
>>
>> If we test it with n = 35, the inner loop would do this:
>>
>> x = 2
>> Test if 2 is a factor: does 35/2 have remainder zero? No.
>> x = 3
>> Test if 3 is a factor: does 35/3 have remainder zero? No.
>> x = 4
>> Test if 4 is a factor: does 35/4 have remainder zero? No.
>> x = 5
>> Test if 5 is a factor: does 35/5 have remainder zero? Yes.
>> So 35 is not prime, and 35 = 5 * (35/5) = 5 * 7
>>
>>
>>
>> Notice that this does more work than necessary! Can you see what work it
>> does that is unnecessary?
>>
>> (Hint: what even numbers are prime?)
>>
>>
>>
>>
>>
> I don't understand the questions either, but I can point out one thing
> that might be puzzling the OP:
>
> When n is 2, the inner loop does nothing, it just skips to the else
> clause.  The reason is that range(2,2) is a null iterator.  range(i,j)
> produces values from i to j-1, or to put it another way values for which
>i <= n < j
>
> If i and j are identical, there's nothing to match it.
>
>
> --
> DaveA
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Classes in multiple files - code included

2013-04-03 Thread Phil

On 04/04/13 11:58, Steven D'Aprano wrote:

On 04/04/13 12:47, Phil wrote:


And this is the error message;

"global name 'Ui_satListDialog' is not defined"


On its own, that is almost useless.

Python gives you more debugging information than that: it gives you a
complete traceback, which includes the actual line of code causing the
problem. We don't even know which file gives the error, let alone which
line of code!

When you try to run the program, you will get an error. Please copy and
paste the *complete* traceback, starting with the line "Traceback (most
recent call last)" all the way to the bottom. It will show you which
file contains the error, what type of error it is, and which line fails.



Thank you for your reply Steven,

As useless as the error message may be it's the only one given.

phil@Asus:~/Eric/Pest$ python pest.py
Traceback (most recent call last):
  File "/home/phil/Eric/Pest/ui/mainwindow.py", line 57, in 
on_actionList_triggered

dialog = Ui_satListDialog.SatelliteListDialog(self)
NameError: global name 'Ui_satListDialog' is not defined
^CTraceback (most recent call last):
  File "pest.py", line 9, in 
sys.exit(app.exec_())

--
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Classes in multiple files - code included

2013-04-03 Thread Peter Otten
Phil wrote:

> I think I must be very close now so I'll post just the code that I think
> is relevant.
> 
> This the main window class:

> import satListDialog

Here you are importing the module "satListDialog"
 
> class MainWindow(QMainWindow, Ui_MainWindow):
> 
> This is the function to show the dialog;
> 
> def on_actionList_triggered(self):
> 
> self.dialog = Ui_satListDialog.SatelliteListDialog()
> self.dialog.show()

and here you are referring to the module "Ui_satListDialog". I'd guess that 
should be

self.dialog = satListDialog.SatelliteListDialog()

instead.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor