[Tutor] question about object oriented programming and inheritance using datetime module
hey guys, I've been experimenting with Python's datetime module, and I created a function that, given a person's birthdate, calculates how old that person is. Now I want to create a class called age_calculator that does much the same thing. My class inherits from the date class, but I have to type 'from datetime import date' before I can initialize the class definition. Is there some way to avoid this ? Also, once I type the import statement and initialize my class definition, I can create an instance of age_calculator. The instance of age_calculator stores the given birthdate, and gives me a infinite loop traceback when I call self.today(). If I don't inherit from date, I would need to put the import statement somewhere inside a method, and I don't recall ever seeing that done. Part of me feels like that's not as elegant as defining an age_calculator class that inherits from datetime.date, but I'm not sure how to do this. For what it's worth, here's my pseudocode (that inherits from date module) and working code (that does not inherit from date module): from datetime import date class age_calculator(date): def __init__(self, year, month, day): time_delta = self.today() - self number_of_years = time_delta.days / 365 return number_of_years class age_calculator: def __init__(self, year, month, day): self.year = year self.month = month self.day = day def calculate_age(self): from datetime import date birth_date = date(self.year, self.month, self.day) date_today = date.today() time_delta = date_today - birth_date number_of_years = time_delta.days / 365 return number_of_years age_calculator(1964, 9, 27).calculate_age() 42 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about object oriented programming and inheritance using datetime module
[EMAIL PROTECTED] wrote: > hey guys, I've been experimenting with Python's datetime module, and I > created a function that, given a person's birthdate, calculates how > old that person is. Now I want to create a class called > age_calculator that does much the same thing. My class inherits from > the date class, but I have to type 'from datetime import date' before > I can initialize the class definition. Is there some way to avoid > this ? No. In order to use class inheritance syntax, class some_class(some_object): some_object must be defined. Otherwise you'll get a variable not defined error. You could make your own class called 'date' and inherit from that, but what would be the point of that? > > Also, once I type the import statement and initialize my class > definition, I can create an instance of age_calculator. The instance > of age_calculator stores the given birthdate, and gives me a infinite > loop traceback when I call self.today(). If I don't inherit from > date, I would need to put the import statement somewhere inside a > method, and I don't recall ever seeing that done. What makes you think you'd have to import within a method? Import datetime.date into the global namespace. That's perfectly alright. Especially since your class depends on it being available. > Part of me feels like that's not as elegant as defining an > age_calculator class that inherits from datetime.date, but I'm not > sure how to do this. For what it's worth, here's my pseudocode (that > inherits from date module) and working code (that does not inherit > from date module): The way you'd go about doing this is to make an extra function that is unique to your inherited class (for example, calculate_age). > > from datetime import date > > class age_calculator(date): > def __init__(self, year, month, day): > time_delta = self.today() - self > number_of_years = time_delta.days / 365 > return number_of_years This init method is overriding the init of the inherited date class. The reason today() doesn't work is probably because of this. > > class age_calculator: > def __init__(self, year, month, day): > self.year = year > self.month = month > self.day = day > > def calculate_age(self): > from datetime import date > birth_date = date( self.year, self.month, self.day) > date_today = date.today() > time_delta = date_today - birth_date > number_of_years = time_delta.days / 365 > return number_of_years > Just move the import outside of the class. from datetime import date class age_calculator: def __init__(self, year, month, day): self.year = year self.month = month self.day = day def calculate_age(self): birth_date = date( self.year, self.month, self.day) date_today = date.today() time_delta = date_today - birth_date number_of_years = time_delta.days / 365 return number_of_years I don't think you really want to inherit from date. HTH, -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about object oriented programming and inheritance using datetime module
[EMAIL PROTECTED] wrote: > hey guys, I've been experimenting with Python's datetime module, and I > created a function that, given a person's birthdate, calculates how old > that person is. Now I want to create a class called age_calculator that > does much the same thing. Why? You have a perfectly good function that does what you want, there is no need to turn it into a class. One of the strengths of Python is that not everything needs to be a class. > My class inherits from the date class, but I > have to type 'from datetime import date' before I can initialize the > class definition. Is there some way to avoid this ? No, and really there is no reason to want to avoid this. You have to import any external module that you want to use directly. Imports are very common in Python code and there is no reason not to use them. Inheriting from date isn't a very good idea. You should inherit from date if your class will be a specialized kind of date. I guess you could think of your class as adding an age() method to date, but you are thinking of it as a calculator. Also date objects are immutable which makes it harder to create a subclass of date. (You have to override __new__() rather than __init__(), but explaining that will probably just confuse you at this point.) There are a few things in your code that are a bit confused - you need to call the base class __init__() in your __init__() method, and __init__() does not return a value. You should probably read up a bit more on classes in Python, either a book or one of the on-line tutorials. > > Also, once I type the import statement and initialize my class > definition, I can create an instance of age_calculator. The instance of > age_calculator stores the given birthdate, and gives me a infinite loop > traceback when I call self.today(). If I don't inherit from date, I > would need to put the import statement somewhere inside a method, and I > don't recall ever seeing that done. It is OK to put an import inside a function or method, but why do you think you need to do this? > Part of me feels like that's not as > elegant as defining an age_calculator class that inherits from > datetime.date, but I'm not sure how to do this. For what it's worth, > here's my pseudocode (that inherits from date module) and working code > (that does not inherit from date module): > > from datetime import date > > class age_calculator(date): > def __init__(self, year, month, day): > time_delta = self.today() - self > number_of_years = time_delta.days / 365 > return number_of_years I'm not sure why this gives an infinite loop (not even sure what an "infinite loop traceback" is), but you have not initialized the base class so the year, month, day parameters are not used at all. > > class age_calculator: > def __init__(self, year, month, day): > self.year = year > self.month = month > self.day = day > > def calculate_age(self): > from datetime import date > birth_date = date( self.year, self.month, self.day) > date_today = date.today() > time_delta = date_today - birth_date > number_of_years = time_delta.days / 365 > return number_of_years This is better, but compare it to your functional version and you should see why the function is preferred. Kent > > age_calculator(1964, 9, 27).calculate_age() > 42 > > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is Python the language for me?
Andy, Disclaimer: I'm a Python newbie, although I've been developing software for many years. (In my humble opinion,) Luke makes some critically important points here. The first one has to do with programming ~concepts~. LP> As an introductory language to Computer Science I'd recommend LP> Python. You spend less time trying to understand the language LP> itself and more time learning abstract concepts like how a merge LP> sort works, inheritance, classes, 'self', and things like that. LP> Once you understand all of these things, C++ will be greatly LP> easier to pick up, as should most Object-Oriented languages be. I LP> think if you start with C++, you'll get discouraged. Perhaps not, LP> but that's how it worked with me. ... LP> As it turns out, all of the classes at my school are in C++. But LP> despite the fact that I came here with barely any C++ experience, LP> I had _programming_ experience, and that, as they say, made all LP> the difference. I went from C to C++ and had an extremely difficult time with shifting my thinking to object orientation. Once I learned the basics of O-O programming, I've found it very easy to shift from one language to another. As a contract developer for hire, and in my work as an employee at Tyrell Software, I frequently run into situations where, for whatever reason, I need to use a language that I know nothing about. Example: On my current project at Tyrell Software, the decision was made to use a load-testing package named The Grinder. (I highly recommend The Grinder: http://grinder.sourceforge.net/) The load-testing part of the project was handed to me. I knew I would have some questions about Python, so I asked who our resident Python Guru was. They told me, "You are, now." Sometimes you need to learn fast. Having learned the basic concepts, when going to a new language, I have to devote much of my learning time to language syntax issues, rather than learning the basics all over again. Don't let anyone tell you that language X is superior to language Y. In one particular instance that may be so, but in the grand scheme of things it all evens out. Different languages do different things better and worse than other languages, but the O-O concepts remain the same. (This week, I'm being paid to write Python code, so Python is clearly the superior language. :-) ) LP> ... you can use packages and such to keep your code manageable. LP> It's probably a good idea to keep your code separated from the LP> get-go when you reach the time to make your game, because it'll LP> eventually get to the point where you'll need to break it apart, LP> so you'd be doing yourself a favor. Packages, modules, libraries -- whatever you want to call them -- are the way to go. If you put a little bit of thought into them when writing them, most things can be reused at some future time. Most code you write will be code that you will write over and over. Try to keep your pieces small and generic, except, or course, where you need to do a specific step that applies to your current project only. Good luck on your project. Chuck -- == Chuck Coker, Software Developer[EMAIL PROTECTED] Tyrell Software Corporation http://www.tyrell.com Office: +1 949 458 1911 x 203Cell: +1 714 326 5939 == ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is Python the language for me?
On Sun, 14 Jan 2007, Luke Paireepinart wrote: > Okay, first thing to note: a lot of Python developers prefer wxPython > to Tkinter. I'd second this. I've never used wxPython, because the documentation is not (at least to me) well suited to learning it (as opposed to using it once you already understand the basics). However, the recently published "wxPython in Action," http://www.manning.com/rappin/ , has changed that for me. It's not perfect, but it's very very good. It has made wxPython accessible to me. I've only used wxPython for a few toy demos to try out a few features, but I'm now at the point that I don't plan on using Tkinter any more, and that's all I've used for GUI apps for the past two years. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is Python the language for me?
Andy wrote: > So as you can see I feel that each language has it's own pro's and > con's and I might end up learning them both eventually. Do you all > think Python will work for what I want right now? You don't say, but it sounds like you are just starting out with programming. I strongly second what Luke said - Python is a great language to start with, much easier than C++, and if you sometime decide to learn C++ it will be much easier when you already know how to program. > My biggest concern > with Python is the upkeep involved in trying to distribute your > applications. Do updates to Python usually cause issues? Why would > something like Pygame work with Python 2.4 but not 2.5? Point upgrades such as 2.4 to 2.5 generally keep a high level of backward compatibility for Python code, so a Python program that runs under 2.4 will most likely run the same under 2.5. However, point upgrades do not retain binary compatibility for compiled extensions such as pygame; these extensions need to be rebuilt for the new version. On Windows most Python programmers are not willing / interested / able to compile the extensions themselves, so it is common for binary installers to be distributed for Windows versions of extensions. When a new Python comes out, these have to be updated. The pace of the updates varies widely depending on how active the extension project is and how much the developers care about Windows. For some users the availability of a key extension is what gates the upgrade. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Change the font size of the lines and rectangle on aTkinter Canvas
"Asrarahmed Kadri" <[EMAIL PROTECTED]> wrote > I want to provide with a functionality of changing the font size, in > this > case the width of the lines. Its not really the font size. fonts only apply to text, its only the line width you want to change. However todo that I think the easiest way is just to elete the existing line (and maybe the whole graph) and redraw with the new parameters. Provideed the data has been precalculated then this should be a rapid operation. > Is it possible to do it dynamically; I mean the graph is > already drawn, now with a Menu, suppose I want to > provide the user with options of 50%, 100% > and 200% font size. You will need to store the line thicknesses in variables so that the drawing routine can be parameter driven. Then simply delete the existing graph and redraw it with the new thickness - remember you may need to rescale your axis to cater for thicker/thinner lines. If you are using a filled Rectangle rather than a real line (which is what I would actually expect to see in a bar chart) then obviously the line thickness simply involves setting the difference in the y coordinates appropriately. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] [Fwd: Re: Is Python the language for me?]
Alan accidentally e-mailed me directly.. forwarding to list! :) --- Begin Message --- Andy wrote: Hey everyone, I'm looking to get the opinions of some people with more experience then myself. Currently I only have time to learn one language and I'm torn between C++ and Python. If you have time to learn C++ then you have time to learn at least two other languages! C++ is one of the hardest languages to learn, in fact its one of only three languages that I actually recommend formal training for (the others are COBOL and Ada) I would honestly rather use Python then C++ but I have a few concerns. How hard is it to manage a large project in Python vs. C++? Python will scale up to anything a single programmer will ever attempt - say around 100,000 lines of code. That equates to somewhere around 250,000 lines of C++ in my experience. If you plan on hiring other programmers and working on a multi-million line epic then C++ might have an advantage. For singleton projects, provided you think about your file and directory structures Python should be fine. Whatever you do, use a good version control system. cvs and subversion are the favourites for small - medium scale use, even rcs is adequate for single user scenarios. Luke: I think if you have a large project in Python, the equivalent project in C++ would be much harder to manage. Not necesarily true since the C++ header/implementation paradigm and strict typing leads to more easily managed code. But at the cost of requiring a lot more files and a lot more code generally. C++ was designed to make writing large projects easier and in that it is quite successful. more of a hassle to me. Once the C++ program has been compiled it's much easier to distribute. C++ has a much higher learning curve and development time then Python does. Python has a much nicer community built around it then C++ from what I can see though. All good points. Except maybe the C++ community which is, in my experience, very helpful to other professionals, but not so good for beginners. C++ is a very advanced language and its adherents tend to be experienced programming professionals with high expectations of those seeking their help. They will answer questions, but the answer may not be that intelligible to a novice! Once you understand all of these things, C++ will be greatly easier to pick up, as should most Object-Oriented languages be. And again I agree. Becoming familiar with Python first is a good step towards making good use of C++/Java/Delphi/Lisp/Smalltalk/ ObjectiveC etc etc. Of course this wouldn't apply to everybody, and the fact that I was fairly young when I tried to learn C++ I'm sure had an effect. I used C++ for many years after about 5 years of C programming and it was still a big jump. C++ requires a lot of low level knowledge about how it works internally to get the best from it and avoid wierd bugs. Just my two cents worth, Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld --- End Message --- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor