[Tutor] intefaces in python
Hi , I don't suppose python has a concept of interfaces. But can somebody tell me if their is a way i can implement something like a java interface in python. -- A-M-I-T S|S ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
"Amit Sethi" wrote Hi , I don't suppose python has a concept of interfaces. Yes and No, I'll come back to that... But can somebody tell me if their is a way i can implement something like a java interface in python. First, can you tell me why you would want to? Java style interfaces tend to make your code much less reusable and much less flexible if you are using a dynamically typed language. There are very few real advantages to them over defining, say, a mixin or using multiple inheritance (which of course Java can't do) The normal way of defining a Java style Python interface class is simply to define a class that has a set of methods thaty either do or return nothing or raise some kind of NotImplementedError exception. But usually providing a mixin is a much more powerful style of programming in Python since you can provide partial implementations of the methods or generic methods that are not dependant on the types of the parameters. Coming back to the original question, Python has a very strong concept of an interface, that is how it checks types, if an interface does not exist it will raise a TypeError. But it does it at runtime and it does it at the method level not the class level. Very different to Microsoft's concept which was designed to meet the needs of COM and was subsequently adopted by Java. There has also been talk of introducing syntax to create interfaces into Python which I personally think is a very, very poor idea! But quite a lot of what I think are poor ideas get intro Python so that doesn't mean much! :-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] syntax error
"Christopher Altieri" wrote loaded python 3 and 3.1 several times on vista. tried first command: print "hello world' but keep getting syntax error. what am I doing wrong? Using Python 3 to learn Python! :-) Seriously, You would be better downgrading to Python 2.6 to learn because v3 has introduced several new concepts (not just print() ) that are not covered in most tutorials. Once you understand Python 2.6 you will be in a better position to understamd v3s new features, and probably by then most tutorials will have caught up. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ And for V3: http://www.alan-g.me.uk/l2p/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
On Sun June 28 2009 1:48 pm, Alan Gauld wrote: > advantages to them over defining, say, a mixin Noob, here, wanted to know what a mixin is eh, just getting back into learning python googled it, still not sure what it is, but wikipedia says the name come from ice cream mixins at Steve's Ice Cream Parlor Ah yes, i was a frequenter there Will watch out for what it is when I get farther into python may have to name them things like jimmies and heath_bar_crunch -- Bob Rea mailto:gapet...@stsams.org http://www.petard.us http://www.petard.us/blog http://www.petard.us/gallery Where is Bill Stringfellow now that we really need him? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Tkinter Button Issue
Hi, I was hoping someone may be able to help me on an issue I'm having with Tkinter buttons; or more specifically, the background and foreground settings. I've created a program that is supposed to simulate an order system: the first section (frame) is where the user adds items to the order and the second section (frame) is where payment is processed. The second section only activates when the first section has been confirmed by the user; so in the meantime all buttons in the second section are disabled. They are then activated so the user can complete the transaction and the first section is disabled so the order can't be edited during payment. It all seems to be working great except that when a button is 'activated' its appearance loses its foreground and background properties, appearing with the default greyish background and black font. As soon as the mouse hovers over the buttons the colour corrects itself. Does anyone have any idea how I can ensure that the colours appear correctly as soon as the button is activated? Perhaps some way to 'programatically' focus the mouse on the buttons? Sorry if this sounds idiotic, I'm obviously still a newbie. :-) Thanks for your time. Adam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] freeze
HI Guys, I have a small programme, called shop.py, that I wish to make into a "frozen binary" ( I think that's right - I'm using Linux Ubuntu 9.04 and I want the programme to work on a windows computer that doesn't have Python installed). I used freeze.py from examples/Tools and everything seemed to be going well (I won't post the output as there is so much), but the last lines are: o M_xml__sax__xmlreader.o M_xmllib.o M_xmlrpclib.o /usr/lib/python2.6/config/libpython2.6.a -L/usr/lib -lz -lpthread -ldl -lutil -lm -o shop /usr/bin/ld: cannot find -lz collect2: ld returned 1 exit status make: *** [shop] Error 1 Any ideas of what is going wrong? I would also like to ask your opinion - the programme is very small (only 1.2kb!). Is there another way ? Am I totally wasting my time? Many thanks Colin ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] character counter
julie wrote: Hello, I need help with the following problem: *Write a loop that reads each line of a file and counts the number of lines that are read until the total length of the lines is 1,000 characters. Use a break statement to make sure that you don't continue reading the file once the 1,000 characters are read. I figured out how to open a file, count and print the lines, however I cannot figure out or find online ...or anywhere else how to count characters in a file. This is what i have so far: file = open("/Users/meitalamitai/Documents/Computer Science/Python/Homework/Lorem_Ipsum.py") lines = 0 for line in file: lines=lines+1 print '%r has %r lines' % ("Lorem_Ipsum.py", lines) if char >= 1000: break * Thanks! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Depends what you classify as a character. If it is any character then what Emile said about len(line) will be fine, if it is to exclude new-line characters then you will need to strip them off and then do a length or if it is just printable characters then maybe look at string translation tables and the string.replace methods to cull out what you do not need. -- Kind Regards, Christian Witts ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter Button Issue
Adam Cunningham wrote: > Hi, I was hoping someone may be able to help me on an issue I'm having > with Tkinter buttons; or more specifically, the background and > foreground settings. > > I've created a program that is supposed to simulate an order system: the > first section (frame) is where the user adds items to the order and the > second section (frame) is where payment is processed. The second section > only activates when the first section has been confirmed by the user; so > in the meantime all buttons in the second section are disabled. They are > then activated so the user can complete the transaction and the first > section is disabled so the order can't be edited during payment. > > It all seems to be working great except that when a button is > 'activated' its appearance loses its foreground and background > properties, appearing with the default greyish background and black > font.. As soon as the mouse hovers over the buttons the colour corrects > itself. A Button (and most other widgets) has three `state`: "normal" (clickable), "active" (mouse hover), and "disabled". "active" in Tk's sense means you hover your mouse on the button, not making the button clickable (which is the "normal" state). These are the properties of a button: | STANDARD OPTIONS | | activebackground, activeforeground, anchor, | background, bitmap, borderwidth, cursor, | disabledforeground, font, foreground | highlightbackground, highlightcolor, | highlightthickness, image, justify, | padx, pady, relief, repeatdelay, | repeatinterval, takefocus, text, | textvariable, underline, wraplength | | WIDGET-SPECIFIC OPTIONS | | command, compound, default, height, | overrelief, state, width You need to change the `background` property to a color of your choosing instead of the `activebackground`. `activebackground` is for background color when the button is hovered. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
Bob Rea wrote: > On Sun June 28 2009 1:48 pm, Alan Gauld wrote: >> advantages to them over defining, say, a mixin > > Noob, here, wanted to know what a mixin is > eh, just getting back into learning python > googled it, still not sure what it is, but > wikipedia says the name come from ice cream mixins at > Steve's Ice Cream Parlor > > Ah yes, i was a frequenter there > > Will watch out for what it is when I get farther into python > may have to name them things like jimmies and > heath_bar_crunch > In object-oriented programming languages, a mixin is a class that provides a certain functionality to be inherited by a subclass, while not meant for instantiation (the generating of objects of that class). Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins through multiple inheritance. -- http://en.wikipedia.org/wiki/Mixin In regular inheritance, you inherit from another class to inherit its interfaces. In mixin inheritance, you inherit for its implementations. The mixin concept takes it a bit further; the mixin class (parent class) may not be usable by itself (i.e. instantiating a mixin class may not make any sense) and must be inherited . Example: # Flavours class Vanilla(object): pass class Chocolate(object): pass # Additions class Nuts(object): pass class Cookies(object): pass # Items class IceCream(): pass # Usable classes class ChocolateIceCream(Chocolate, IceCream): pass class VanillaAndNutsIceCream(Vanilla, Nuts, IceCream): pass class ChocolateAndCookiesIceCream(Chocolate, Cookies, IceCream): pass Vanilla(), Chocolate(), Nuts(), and Cookies() are not meant to be instantiated directly; they are meant to be inherited. These classes are called mixin classes. In python's standard lib, an example of mixin class is threading.Thread() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor