[Tutor] enhanced subtration in an exponent
Why does the compiler choke on this? It seems to me that the enhanced subtraction resolves to a legitimate integer in the exponent, but I get a syntax error: B = '11011101' sum = 0 start = len(B) for char in B: sum += int(char) * 2**(start -= 1) ## syntax error print(sum) -- Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] calling a method directly
Is there any difference between these two since they give the same result, and when is the second preferred? >>> x = 'ABE' >>> x.lower() 'abe' >>> str.lower(x) 'abe' -- Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] what's wrong with this ?
>>>start_time = "2014-7-1" >>> revlines = commands.getoutput("git log --pretty=format:'%ad:%an' --date=short --since='%s' --no-merges" %start_time).strip().split('\n') Traceback (most recent call last): File "", line 1, in ValueError: unsupported format character 'a' (0x61) at index 26 >>> if I directly use revlines = commands.getoutput("git log --pretty=format:'%ad:%an' --date=short --since='2014-7-1' --no-merges" ).strip().split('\n') it works well ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] enhanced subtration in an exponent
On Apr 21, 2015 12:24 AM, "Jim Mooney" wrote: > > Why does the compiler choke on this? It seems to me that the enhanced > subtraction resolves to a legitimate integer in the exponent, but I get a > syntax error: > > B = '11011101' > sum = 0 > start = len(B) > for char in B: > sum += int(char) * 2**(start -= 1) ## syntax error Assignment is a statement. In Python, statements can't be composed. In the last line, it looks like you're trying to do two assignments, in incrementing 'sum' and decrementing 'start'. Try separating that into two separate statements. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] what's wrong with this ?
On Apr 21, 2015 12:27 AM, "lei yang" wrote: > > >>>start_time = "2014-7-1" > >>> revlines = commands.getoutput("git log --pretty=format:'%ad:%an' > --date=short --since='%s' --no-merges" %start_time).strip().split('\n') > Traceback (most recent call last): > File "", line 1, in > ValueError: unsupported format character 'a' (0x61) at index 26 Your format string has an embedded format string in it. Although you may think it obvious that Python should know not to touch the embedded format string since there are single quotes, Python doesn't take those single quote characters with any reverence. Therefore, it can't tell that you want to keep the embedded one as a literal. Fundamentally, building a command string with string formatting is error prone: consider the subprocess module and passing an explicit list of arguments, rather than a single string. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calling a method directly
On 21/04/15 05:19, Jim Mooney wrote: Is there any difference between these two since they give the same result, and when is the second preferred? x = 'ABE' x.lower() 'abe' str.lower(x) 'abe' They are essentially the same method being accessed in two different ways. The first via the instance, the second via the class. It's a bit like when you call a superclass method in OOP: >>> class C: ... def f(s): print 'in C' ... >>> class D(C): ... def f(s): ... C.f(s) ... print 'and D' ... def g(s): print 'only D' ... In the first line of D.f() you invoke C's foo method by referring to C and passing the local self as the object. You can do it in top level code too: >>> d = D() >>> d.f() in C and D >>> C.f(d) in C >>> >>> d.g() only D >>> D.g(d) only D There are very few cases where the class version is preferred, you nearly always use the instance technique. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] what's wrong with this ?
On 21/04/15 06:22, lei yang wrote: start_time = "2014-7-1" revlines = commands.getoutput("git log --pretty=format:'%ad:%an' --date=short --since='%s' --no-merges" %start_time).strip().split('\n') Traceback (most recent call last): File "", line 1, in ValueError: unsupported format character 'a' (0x61) at index 26 Your pretty=format argument has a %a in it. Python tries to match percent signs to the values provided but it doesn't know how to process %a. You need to get a literal % sign into your string so you need to double it up. Here is a simpler example: >>> "foo %%5 and %d" % 45 'foo %5 and 45' >>> Alternatively use the more modern subprocess module while helps prevent these kinds of issues by taking all arguments as separate strings in a list. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] enhanced subtration in an exponent
On 21/04/15 01:44, Jim Mooney wrote: Why does the compiler choke on this? It seems to me that the enhanced subtraction resolves to a legitimate integer That's your mistake. Assignment is a statement not an expression. It does not return anything. In fact its just illegal to try: >>> print x=3 File "", line 1 print x=3 ^ SyntaxError: invalid syntax >>> You have to do all the assignments up front. If you don;t need to store the new value you can just put the subtraction as an argument: sum += int(char) * 2**(start-1) But if you need start to hold that new value you must do that assignment before using it. BTW. This is 'A Good Thing' since it avoids a whole class of bug that is very common in languages like C and encourages breaking up complex statements into multiple lines which aids debugging, readability and maintenance. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calling a method directly
Jim Mooney wrote: > Is there any difference between these two since they give the same result, > and when is the second preferred? > x = 'ABE' x.lower() > 'abe' str.lower(x) > 'abe' You may call str.lower() explicitly for subclasses of str. If the subclass overrides the lower() method there is a difference: >>> class MyStr(str): ... def lower(self): return "lower({!r})".format(self) ... >>> x = MyStr("ABC") >>> x.lower() "lower('ABC')" >>> str.lower(x) 'abc' I don't think the second form is ever preferred, but if you know in advance that you are dealing only with strings a lazy person may sometimes write str.lower instead of the more conventional def lower(s): return s.lower() >>> words = "abrufen anrufen Anrufer".split() >>> sorted(words) ['Anrufer', 'abrufen', 'anrufen'] >>> sorted(words, key=str.lower) ['abrufen', 'anrufen', 'Anrufer'] >>> sorted(words, key=lambda s: s.lower()) ['abrufen', 'anrufen', 'Anrufer'] The main disadvantage of invoking an unbound method is that it defeats "duck-typing" (the code works with every object that has a lower() method returning a string), and that is more common and more useful than you might think after reading the following example: >>> class Cow: ... def lower(self): return "moo" ... def __repr__(self): return "" ... >>> words.append(Cow()) Lisa provides the interface needed for the sort operation >>> sorted(words, key=lambda s: s.lower()) ['abrufen', 'anrufen', 'Anrufer', ] but is not exactly a str: >>> sorted(words, key=str.lower) Traceback (most recent call last): File "", line 1, in TypeError: descriptor 'lower' requires a 'str' object but received a 'Cow' Moo ;) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Pun panic, was Re: calling a method directly
Peter Otten wrote: class Cow: > ... def lower(self): return "moo" If you can't make sense of that: at the time of writing I thought that "lowering" was a synonym for "mooing". After a look into the dictionary it turns out to be "lowing", not "lowering". Sorry! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pun panic, was Re: calling a method directly
On 21/04/15 09:45, Peter Otten wrote: Peter Otten wrote: class Cow: ... def lower(self): return "moo" If you can't make sense of that: at the time of writing I thought that "lowering" was a synonym for "mooing". After a look into the dictionary it turns out to be "lowing", not "lowering". Sorry! :-) I thought you were just being a bit random! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] introspection
On 04/21/2015 01:21 AM, Danny Yoo wrote: What's supposed to happen in this situation? ## class Person(object): def __init__(self): pass j = Person() john = j jack = j ## What single name should we get back from the single Person object here? "j", "john", or "jack"? And what name should you get from the second Person() object created here? mylist = [Person(), Person(), Person()] -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] enhanced subtration in an exponent
On 04/20/2015 08:44 PM, Jim Mooney wrote: Why does the compiler choke on this? It seems to me that the enhanced subtraction resolves to a legitimate integer in the exponent, but I get a syntax error: B = '11011101' sum = 0 start = len(B) for char in B: sum += int(char) * 2**(start -= 1) ## syntax error print(sum) As others have said, the augmented assignment, like the regular assignment, is not permissible inside an expression. It is a type of statement. You could solve this easily enough by: B = '11011101' sum = 0 start = len(B) for index, char in enumerate(B): sum += int(char) * 2**(start - index) print(sum) But I'd think that: B = '11011101' sum = 0 for char in B: sum = sum * 2 + int(char) print(sum) reads much better, as well as being much faster. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is it possible to "backport" the datetime module of Python 3.3 to Python 3.2?
On 20/04/2015 21:05, Albert-Jan Roskam wrote: Hi, My Raspberry Pi 2 comes with Python 3.2 (and 2.7). I run some code that uses the datetime module but I get an error: "AttributeError: 'datetime.datetime' object has no attribute 'timestamp'". On https://docs.python.org/3/whatsnew/3.3.html I see: "New datetime.datetime.timestamp() method: Return POSIX timestamp corresponding to thedatetime instance." Is upgrading Python 3.3 or higher my only option, or is it somehow possible to use the newer version of the datetime library for Python 3.2? I do not want to modify the source code of the library that causes the error (pysolar). Thanks! Regards, Albert-Jan Python is very strong in guaranteeing backward compatibility, so why not copy the 3.3 pure Python code to your 3.2 setup and see what happens? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] bin to dec conversion puzzlement
On 20/04/2015 21:47, Ben Finney wrote: Jim Mooney writes: I can't seem to get my head around this 'simple' book example of binary-to-decimal conversion, which goes from left to right: B = '11011101' I = 0 while B: I = I * 2 + int(B[0]) B = B[1:] print(I) 221 That is, IMO, a needlessly confusing way to write that code. Whoever wrote it is clearly pleased with how clever it is; but cleverness is almost always a property of *bad* code because it's difficult to understand at a glance. That's the case here. One significant problem with the code as written is that it uses a ‘while’ loop and mutates the list, where there's no point; it should just iterate over items *from* the list without changing it. Another significant problem is that it uses moronically-short, completely unexpressive names. This is Python not FORTRAN. Try this:: binary_text = '11011101' result = 0 for binary_digit in binary_text: # Accumulate powers of 2 for each digit. result = result * 2 + int(binary_digit) print(result) Both methods work but I just can't see how the first one does. Am I missing something obvious here? No, you were missing something needlessly obscured by the badly-written code. Which book is this? I will be sure never to recommend it. Hope that helps. I agree entirely so a big +1 from me. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is it possible to "backport" the datetime module of Python 3.3 to Python 3.2?
- Original Message - > From: Mark Lawrence > To: tutor@python.org > Cc: > Sent: Tuesday, April 21, 2015 1:31 PM > Subject: Re: [Tutor] Is it possible to "backport" the datetime module of > Python 3.3 to Python 3.2? > > On 20/04/2015 21:05, Albert-Jan Roskam wrote: > >> Hi, >> >> My Raspberry Pi 2 comes with Python 3.2 (and 2.7). I run some code that > uses the datetime module but I get an error: >> >> "AttributeError: 'datetime.datetime' object has no attribute > 'timestamp'". On https://docs.python.org/3/whatsnew/3.3.html I see: > "New datetime.datetime.timestamp() method: Return POSIX timestamp > corresponding to thedatetime instance." Is upgrading Python 3.3 or higher > my only option, or is it somehow possible to use the newer version of the > datetime library for Python 3.2? I do not want to modify the source code of > the > library that causes the error (pysolar). >> >> Thanks! >> >> >> Regards, >> >> Albert-Jan >> >> > > Python is very strong in guaranteeing backward compatibility, so why not > copy the 3.3 pure Python code to your 3.2 setup and see what happens? Hmmm, nice idea. Never thought about that. Well, last night (before I saw your reply) I decided to download & compile Python 3.4, because I was afraid that this error might be the tip of the iceberg. Luckily, compiling the code went surprisingly fast on that little computer! I had to do it twice, though, because I got an error about _ssl (maybe with ensurepip?). I fixed it using "sudo apt-get install libssl-dev", but isn't the following the Offical Way (tm) to do this? Are there any more errors lurking in my Python 3.4 now? *) How do I find out the URL of the entry? pi@raspberrypi ~ $ sudo apt-get build-dep python3.4 Reading package lists... Done Building dependency tree Reading state information... Done E: You must put some 'source' URIs in your sources.list *) Argh, while writing this I can confirm that this is still not a good installation: pi@raspberrypi ~ $ python3.4 -c "import sqlite3" Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.4/sqlite3/__init__.py", line 23, in from sqlite3.dbapi2 import * File "/usr/local/lib/python3.4/sqlite3/dbapi2.py", line 27, in from _sqlite3 import * ImportError: No module named '_sqlite3' Probably this will work, but I will *never* be able to remember all these packages, so an entry in sources.list will be much nicer: https://github.com/yyuu/pyenv/wiki/Common-build-problems Best wishes, Albert-Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is it possible to "backport" the datetime module of Python 3.3 to Python 3.2?
On Tue, Apr 21, 2015 at 12:31:53PM +0100, Mark Lawrence wrote: > Python is very strong in guaranteeing backward compatibility, so why not > copy the 3.3 pure Python code to your 3.2 setup and see what happens? Normally backwards compatibility refers to the other way: 3.3 will run 3.2 code. To have 3.2 run 3.3 code is *forward compatibility*. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pun panic, was Re: calling a method directly
Good morning underscore underscore Peter underscore underscore, class Cow: ... def lower(self): return "moo" If you can't make sense of that: at the time of writing I thought that "lowering" was a synonym for "mooing". After a look into the dictionary it turns out to be "lowing", not "lowering". Sorry! I, too, assumed an amusingly random example. I simply imagined a maladjusted bovine Lisa suspended ominously beneath the clouds. Your Lisa must not be just a lowing (synonym 'mooing') cow, she must also be a lowering cow. to lower (v.i.), to be or become dark, gloomy, and threatening (which is cognate to the contemporary German word 'lauern') And, I dairy not chase this pun any further -Martin [0] http://www.merriam-webster.com/dictionary/lower -- Martin A. Brown http://linux-ip.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pun panic, was Re: calling a method directly
On Tue, Apr 21, 2015 at 7:09 AM, Martin A. Brown wrote: > And, I dairy not chase this pun any further > No - keep milking it. It gets butter all the time. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] introspection
On 2015-04-20 22:21, Danny Yoo wrote: What's supposed to happen in this situation? ## class Person(object): def __init__(self): pass j = Person() john = j jack = j ## What single name should we get back from the single Person object here? "j", "john", or "jack"? I was hoping that it would be possible to create a function that would do the following: def my_name(some_object): return some_object.__name__ so, using what you entered above.. my_name(j) 'j' my_name(john) 'john' my_name(jack) 'jack' But I see what I think you and others have been trying to explain to me: that the expression some_object.__name__, if it existed, would indeed be schizophrenic since it would be an attribute of the object, not the name(s) to which it is bound. Thanks all for your replies. ak ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] introspection
> But I see what I think you and others have been trying to explain to me: > that the expression some_object.__name__, if it existed, would indeed be > schizophrenic since it would be an attribute of the object, not the name(s) > to which it is bound. The hypothetical feature might also, if designed badly, break simple things like checking for equality. e.g. say that we do: x = # ... some value y = copy.deepcopy(x) Would x == y? From my understanding, if the object were automatically associating a __name__, it should not. And that would be weird. :P This is not to say that what you're asking for is unreasonable! It's just that Python doesn't do it, and introducing such a feature in Python would have large implications in terms of what it means, not to mention how it might affect the runtime. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] handling exception raising in atexit registered function
Hi, I am developing an automated testing script in python which will, among other things, send an email when it is done. For this I would like to send an email when an exception is raised as well - since it already has a cleanup function registered with atexit, this is the logical place to do it. However, I didn't find a good HOWTO on a cleanup function like that identifying an exception has been raised and reacting to it. thanks, Opher ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] ?!
Hi, Didn't really understand the instructions on the website but is this the right email address for help? Thanks, cory :) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ?!
On 04/21/2015 04:17 AM, Ryan Scholes wrote: Hi, Didn't really understand the instructions on the website but is this the right email address for help? It can be... Some very smart, experienced and helpful folks here - some are even all three! ;) Generally speaking... they aren't going to do your (home) work for you... try to provide some code to show what you've tried, and specific questions as to what you're having trouble with. -- Shiny! Let's be bad guys. Reach me @ memilanuk (at) gmail dot com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ?!
On 21/04/15 12:17, Ryan Scholes wrote: Didn't really understand the instructions on the website but is this the right email address for help? If its help about: a) learning to program b) learning the Python programming language c) learning the Python standard library Then yes, this is the right place. Ask questions. Tell us what OS you use, what version of Python and if you have an error message cut n paste it into the email. We will try to answer for you or at least point you in the right direction. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pun panic
"Martin A. Brown" writes: > Good morning underscore underscore Peter underscore underscore, The Pythonic way to pronounce that would be “dunder Pete dunder” https://wiki.python.org/moin/DunderAlias>. -- \ “Geeks like to think that they can ignore politics. You can | `\leave politics alone, but politics won't leave you alone.” | _o__) —Richard M. Stallman, 2002-07-26 | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] introspection
Alex Kleider writes: > I was hoping that it would be possible to create a function > that would do the following: > > def my_name(some_object): > return some_object.__name__ That hope is understandable. It is also easy to be confused about why such a feature doesn't exist; after all, it works for functions and classes and modules (oh my!):: >>> def foo(): pass ... >>> foo.__name__ 'foo' >>> class Bar: pass ... >>> Bar.__name__ 'Bar' >>> import sys >>> sys.__name__ 'sys' So why not arbitrary objects? >>> spam = 4 >>> spam.__name__ Traceback (most recent call last): File "", line 1, in AttributeError: 'int' object has no attribute '__name__' The answer is that functions, classes, and modules are all *defined*, and (normally) have exactly one canonical name established at definition time. Arbitrary objects are merely *instantiated*, without that definition step. Quite commonly they are used with no name bound to them; so the behaviour of most objects does not have ‘__name__’ in the API. If you would like to make a class that has that attribute on all its instances, feel free. But you need to figure out how the instance detects its own name! class LockeanThing: """ An object that knows the name by which others refer to it. """ def __init__(self): self.__name__ = ??? > But I see what I think you and others have been trying to explain to > me: that the expression some_object.__name__, if it existed, would > indeed be schizophrenic since it would be an attribute of the object, > not the name(s) to which it is bound. That's why I prefer to be clear that the binding operation is one-way only. A reference (such as a name) is bound to an object, the object is not bound to the reference — indeed, the object knows nothing about that relationship. -- \“I washed a sock. Then I put it in the dryer. When I took it | `\ out, it was gone.” —Steven Wright | _o__) | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] introspection
On 21Apr2015 09:59, Alex Kleider wrote: On 2015-04-20 22:21, Danny Yoo wrote: What's supposed to happen in this situation? ## class Person(object): def __init__(self): pass j = Person() john = j jack = j ## What single name should we get back from the single Person object here? "j", "john", or "jack"? I was hoping that it would be possible to create a function that would do the following: def my_name(some_object): return some_object.__name__ so, using what you entered above.. my_name(j) 'j' my_name(john) 'john' my_name(jack) 'jack' But I see what I think you and others have been trying to explain to me: that the expression some_object.__name__, if it existed, would indeed be schizophrenic since it would be an attribute of the object, not the name(s) to which it is bound. But it would not be schizophrenic to write a function that returned a name arbitrarily, by inspecting locals(). It depends whether you only need a name, or if you need "the" name. Write yourself a "find_name_from_locals(local_map, value)" function that reports. That will (a) get you a partial answer and (b) cement in your mind what is possible and why. Easy and useful! Cheers, Cameron Simpson Technique will get you through times of no strength a lot better than strength will get you through times of no technique.- the Nealomatic ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calling a method directly
On 21Apr2015 09:03, alan.ga...@btinternet.com wrote: On 21/04/15 05:19, Jim Mooney wrote: Is there any difference between these two since they give the same result, and when is the second preferred? x = 'ABE' x.lower() 'abe' str.lower(x) 'abe' They are essentially the same method being accessed in two different ways. The first via the instance, the second via the class. Though only because 'x' is directly a str. Not some other class. In Jim's example we _know_ 'x' is a str. In general one is less sure, and often one hopes not to care! It's a bit like when you call a superclass method in OOP: class C: ... def f(s): print 'in C' ... class D(C): ... def f(s): ... C.f(s) ... print 'and D' ... def g(s): print 'only D' ... In the first line of D.f() you invoke C's foo method by referring to C and passing the local self as the object. Worth pointing out that this is almost always the only time you cant to call via the class directly. And that it is actually common in subclass method implementations, particularly __init__, where on must often go: class D(C): def __init__(self, blah...): # initialise the common "C" class related stuff C.__init__(self, ...) # intialise the specific "D" class related stuff ... D-specific-stuff-here ... [...] There are very few cases where the class version is preferred, you nearly always use the instance technique. Indeed. Not least because (a) you don't always know or even care what class an object is and (b) objects themselves can have callable attributes. Cheers, Cameron Simpson Applications Programming is for dullards who can't do Systems Programming. - David Rose, d...@werple.apana.org.au ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is it possible to "backport" the datetime module of Python 3.3 to Python 3.2?
On 21Apr2015 22:50, Steven D'Aprano wrote: On Tue, Apr 21, 2015 at 12:31:53PM +0100, Mark Lawrence wrote: Python is very strong in guaranteeing backward compatibility, so why not copy the 3.3 pure Python code to your 3.2 setup and see what happens? Normally backwards compatibility refers to the other way: 3.3 will run 3.2 code. To have 3.2 run 3.3 code is *forward compatibility*. Yes, but it can depend on where you stand. Mark was talking about taking the 3.3 datetime module code and running it on 3.2. So in this context, he is suggesting that the _code_ in the 3.3 datetime module is probably backward compatible - it has presents features but does not depend on 3.3 aspects of the language, and therefore may well run on 3.2. Cheers, Cameron Simpson The most annoying thing about being without my files after our disc crash was discovering once again how widespread BLINK was on the web. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] introspection
On 2015-04-21 16:48, Cameron Simpson wrote: But it would not be schizophrenic to write a function that returned a name arbitrarily, by inspecting locals(). It depends whether you only need a name, or if you need "the" name. In my use case there'd probably be only one name for the given object so I believe you've given me my next exercise! Write yourself a "find_name_from_locals(local_map, value)" function that reports. That will (a) get you a partial answer and (b) cement in your mind what is possible and why. Easy and useful! I'll get working on that! Cheers, Cameron Simpson Technique will get you through times of no strength a lot better than strength will get you through times of no technique.- the Nealomatic Wonderful quote by the way! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] introspection
On 2015-04-21 16:38, Ben Finney wrote: That hope is understandable. Your "understanding" is appreciated. It is also easy to be confused So true, but with the help of "Python Tutors" things are being rectified! about why such a feature doesn't exist; So why not arbitrary objects? The answer is that functions, classes, and modules are all *defined*, and (normally) have exactly one canonical name established at definition time. Arbitrary objects are merely *instantiated*, without that definition step. Quite commonly they are used with no name bound to them; so the behaviour of most objects does not have ‘__name__’ in the API. If you would like to make a class that has that attribute on all its instances, feel free. But you need to figure out how the instance detects its own name! class LockeanThing: """ An object that knows the name by which others refer to it. """ def __init__(self): self.__name__ = ??? But I see what I think you and others have been trying to explain to me: that the expression some_object.__name__, if it existed, would indeed be schizophrenic since it would be an attribute of the object, not the name(s) to which it is bound. That's why I prefer to be clear that the binding operation is one-way only. A reference (such as a name) is bound to an object, the object is not bound to the reference — indeed, the object knows nothing about that relationship. It's sinking in. Thank you. ak ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pun panic
Ben Finney wrote: > "Martin A. Brown" writes: > >> Good morning underscore underscore Peter underscore underscore, > > The Pythonic way to pronounce that would be “dunder Pete dunder” > https://wiki.python.org/moin/DunderAlias>. > Hm, should I campaign for a peter() builtin? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor