output

S

Shi Mu

After I run the following python code, I expect to have the printing such as:
The year is 2005

However, I got something like:
The year is 2005
Fri Oct 14 17:43:31 2005
Fri Oct 14 17:43:31 2005
The year is 2005

What is the reason?

The code follows:

import time
import now

class today(now.now):
def __init__(self, y = 1970):
now.now.__init__(self)
def update(self,tt):
if len(tt) < 9 :
raise TypeError
if tt[0] < 1970 or tt[0] > 2038:
raise OverflowError
self.t = time.mktime(tt)
self(self.t)

if __name__ == "__main__":
n = today()
print "The year is", n.year
 
S

Steven D'Aprano

After I run the following python code, I expect to have the printing such as:
The year is 2005

However, I got something like:
The year is 2005
Fri Oct 14 17:43:31 2005
Fri Oct 14 17:43:31 2005
The year is 2005

What is the reason?

The code follows:

import time
import now
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named now

Looks like your bug is in your module "now". Would you like to tell us
what is in it, or should we try to guess?

Looking at the rest of your code:

class today(now.now):

You have a class called "now" in the module "now", and are sub-classing it
into a class called "today". I think this is bad design.

Firstly, it is recommended practice to name classes with an initial
capital letter. So you would use now.Now as the class.

Secondly, I don't feel that today is something which should be a class.
After all, there is only one thing "today". I feel that today should be a
function that returns the date of today, or maybe an instance of a date
class. Then you could have instances "yesterday", "tomorrow",
"one_week_from_last_wednesday" and so forth.

But let's continue:

def __init__(self, y = 1970):

You are initialising an instance of "today" with a default year of 1970?
Do you have a time machine?

This suggests to me that you are actually trying to create a general date
class. Am I close?
now.now.__init__(self)

Remember, now is a module, now.now is a class from that module. So you
are calling the __init__ method of the now.now class with self as a
parameter. This is very confusing code. Who knows what this is doing --
not us, because you haven't shown us the code.

What are you trying to accomplish here? It almost looks like you are
trying to use the Borg design pattern, by making every instance of today
store it's data inside the superclass. Is this deliberate?

def update(self,tt):
if len(tt) < 9 :
raise TypeError
if tt[0] < 1970 or tt[0] > 2038:
raise OverflowError
self.t = time.mktime(tt)
self(self.t)

Why are you calling self as if it were a function? Does now.now have a
__call__ method?
if __name__ == "__main__":
n = today()
print "The year is", n.year

I would guess that when you create an instance of class today, somewhere
in that mess of calling the superclass, you have a print statement that
prints the current time and date. That piece of code is being called twice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,268
Messages
2,571,344
Members
48,019
Latest member
Migration_Expert

Latest Threads

Top