![]() |
|
Re: ** Python Support Thread **
Code:
class Property(object): |
Re: ** Python Support Thread **
Is the book in Python 2? Because after some googling and looking at documentation I'm not even sure it even does anything in Python 3
|
Re: ** Python Support Thread **
ha just wrote out a long post in quick reply and lost it. :(
it's from this book, http://www.amazon.com/Python-3-Objec...5579972&sr=8-3 he used it in one of the "case studies" at the end of chapter 3 on multiple inheritance, for code that would manage real estate properties. the code is really long and he kept using the same name for the methods in different classes and it made it really hard to understand. here is his explanation, Quote:
Code:
class Person(object): |
Re: ** Python Support Thread **
Quote:
Quote:
Instead of this... Code:
def isSquare(n):Code:
def isSquare(n):RoundTower is right too about the sqrt test being invalid for large numbers. Round off error for floats can cause the sqrt function to return a value that's "supposed" to be x.0 as (x-1).999999 or x.000001 where casting it as an int (or taking it mod 1) may return the "wrong" result. This isn't too big a problem with sqrts, but can be terrible for cube roots and other fractional powers that can't be represented exactly as a float in decimal (actually binary I guess?). You can approximate the test to arbitrary precision with a tolerance by doing something like... Code:
def isSquare(n):ETA: My last code snippet is kinda bad since, given the distribution of square roots, sqrt(n) is more likely to return x.000001 than (x-1).999999 but the point is valid. |
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
Oops. I skimmed and thought the whole post was about problem 39. Ok ignore that part.
|
Re: ** Python Support Thread **
Quote:
After some searching around, I think he must have taught this in reverse order. First, you should have been exposed to "self", "other", and "_init_." What he did was try to show you class functions equivalent to: def myfun(): myfun() instead of def myfun(arg): myfun(x) I think that the staticmethod() function allows you to call a class w/o actually initializing it with arguments. He is also throwing a ton of concepts at you in this code, so it's a tad hard to figure out what or why you are doing what you are doing (or even modifying the code to correctly reflect what he is doing). Also, why did you pick this one up instead of the Lutz book? |
Re: ** Python Support Thread **
Sorry, I wrote that code more for myself to figure out what staticmethod did.
Quote:
Do you like the Lutz book? It has poor reviews on amazon. This book is kind of weird. It is more about designing applications with OOP and the big picture than just listing off every in and out of the language. Specifically it has two chapters on "design patterns" that I haven't come across in other books. |
Re: ** Python Support Thread **
http://pastebin.com/gzBXzjQK
here is the case study that I was having trouble with. no comments because the text explains every couple blocks of code. |
Re: ** Python Support Thread **
Can you copy/paste an actual working code?
It's 191 lines and had an indentation error in it. I fixed it, ran it, and nothing happens. No need for the comments in the code. It's pretty self-explanatory. I added this line to your code: Code:
get_valid_input('abcdef', 'yes')Code:
>>> I don't think it matters which language you use to learn OOP if that is your aim in picking up this book. I know there are entire books dedicated to design patterns, though they usually use more hard-core OOP languages like Java. OOP in Python feels like it was added on with little thought, but maybe that's my opinion. |
Re: ** Python Support Thread **
all the code does is allow an "agent" to add specific properties (house or apartment for rent or purchase) and their details and then display them. either import the module in an IDE or add this,
Code:
agent = Agent() |
Re: ** Python Support Thread **
So, something else I just learned from Project Euler: I started my Problem 44 solution and went to make a sandwich or something and when I came back the answer appeared literally the instant I was going to stop it and try something else. My usual route in this situation is to submit the answer anyway but look at the forum thread to see how it could be improved and saw someone else using Python made a post to the effect of "It's weird, I tried changing the set to a list and suddenly my solution took eight minutes instead!" So I tried the inverse, switching a list to a set and...it went from nearly six minutes to 1.37 seconds! (fwiw I had a list because I mistakenly thought the order mattered at first, though I was never sorting it; I was appending in ascending order as I needed more elements. The only operations I did in either version were appending and iterating)
|
Re: ** Python Support Thread **
I've spent about 20 minutes on this, and I conclude that the staticmethod() is really nothing more than syntactic sugar and perhaps helpful in debugging.
If I comment out the method, as I did in the Rental class: Code:
class Rental:There is another area that hints at this. I'm not sure if you made a transcription mistake or if he wrote it like this, but if this method was not syntactic sugar, this would probably throw an indentation error, but it doesn't: Code:
class Purchase:Code:
def prompt_init():To experiment further, I also added this: Code:
Code:
Code:
I think you can probably comment out all the staticmethods in this code and have it work properly assuming you do not want to call a class by itself. The code isn't well sanitized either. For example, I can answer: "How many square feet:" as k w/o it asking me to enter an integer (if that is what he wanted in this case). I'm sort of mystified at how he managed to get super() to work correctly in this code, since there is no specified inheritance, but super() is a massive mind-****, so it's possible I am confused for no good reason here. Interesting blog linked from SO on super(): http://www.artima.com/weblogs/viewpo...?thread=237121 |
Re: ** Python Support Thread **
I commented out all the staticmethod() and the code seems to work just fine for now.
I want to refactor all of this though: Code:
|
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
Yeah, I was more correct in the earlier post irt to what staticmethod is, but in the case of the code from the book, the staticmethod() doesn't seem to be doing anything vital unless you want to have it there for debugging.
I found this article that seems to explain exactly what this decorator function does: http://arunnambyar.blogspot.com/2010...mentation.html |
Re: ** Python Support Thread **
Quote:
I'm not sure what you could mean by saying the staticmethod call "doesn't seem to be doing anything vital unless you want to have it there for debugging". It's no more or less vital than any other line in the code, and it has nothing whatsoever to do with debugging. |
Re: ** Python Support Thread **
Yeah, staticmethod is definitely not syntactic sugar (unlike the @staticmethod decorator which is, as are all decorators).
This example should make clear the difference between static and non-static methods: Code:
|
Re: ** Python Support Thread **
Interesting stuff. I was trying to reverse engineer the idea, but alas, I was off.
Why does the entire program work just fine when the staticmethods are commented out? I find this confusing. I get conceptually what the staticmethod is doing, but I don't get what the author of this code was trying to demonstrate. I think your guy's examples are much better, because these examples illustrate exactly why it would be needed and exactly what happens if it is not used. Was that a bad example code or am I missing something obvious? I want to rewrite that program and figure out if there is a better way to write it. I'll post it when I am done. |
Re: ** Python Support Thread **
Quote:
There's a lot of threads on this topic: http://stackoverflow.com/search?q=staticmethod |
Re: ** Python Support Thread **
Quote:
The thing is the author wasn't just trying to show what a static method does in this case study. This was at the end of a chapter that covered super(), multiple inheritance and polymorphism. So he threw all that **** in to one stupid example, after expressing that you probably shouldn't use multiple inheritance unless you have to. |
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
Code:
import time |
Re: ** Python Support Thread **
think of it this way: why is 'now' declared inside the function call as opposed to, say, being a global variable?
|
Re: ** Python Support Thread **
Quote:
http://en.wikipedia.org/wiki/Closure_(computer_science) |
| All times are GMT -4. The time now is 06:18 PM. |
|
Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Copyright © 2008-2020, Two Plus Two Interactive