Problem porting class to python3.2

Terry Reedy tjreedy at udel.edu
Thu Jun 2 15:15:22 EDT 2011


On 6/2/2011 12:18 PM, Nick Buchholz wrote:
> Hi all,
>      I've been wandering through the DOCs for an hour and haven't found a solution to this
> I'm just starting to convert from 2.5 to 3.2 and I have a problem. I have a code that looks like this.
>
> from tkinter  import *
> import time
> import datetime
> import string
> import math
> import random
>
> print (time.localtime())
>
> def foo():
>      print (time.localtime())

Be itself, this works fine in 3.2
 >>> import time
 >>> def foo(): print (time.localtime())

 >>> foo()
time.struct_time(tm_year=2011, tm_mon=6, tm_mday=2, tm_hour=15, 
tm_min=3, tm_sec=48, tm_wday=3, tm_yday=153, tm_isdst=1)

Add a foo() call in your code too.

> class StarDate:

The first thing I would do is make this a new-style class in 2.5.
class StartDate(object):
Make sure your class still works with that change.
('(object)' can be removed or left when running under 3.2.)

>      """ implements StarDates regular dates but with output in
>      the form: YYYYMMDD:HHMMSS.FFFF
>      or represented by a 6-Tuple (Yr, Mon, Day, Hour, Min, Sec)
>      """
>      def __init__(self, tTuple=None):
>          tt=self
>          tt.tm_year = tt.tm_mon = tt.tm_mday = tt.tm_hour = 0
>          tt.tm_min  = tt.tm_sec = tt.tm_wday = tt.tm_yday = 0
>          tt.tm_isdst  = 0
>          if type(tTuple) == type(None):
>              tTuple = time.localtime()
>          elif .......
>
> The two print statements work as expected, printing the tuple of the local time.
> The function foo and the StarDate class definition both fail with the error.
>
>     File "starDate.py", line 37 , in foo
>        print(time.localtime())
> NameError: global name 'time' is not defined

> What am I missing?
> why doesn't the definition of time at the top level get recognized inside the class?

Without seeing the complete traceback and all the code up to line 37 and 
everything beyond involved in the call chain, I cannot even guess. No 
possibilities come to mind.

> If I can't get a simple two class file working in 3.2,

Many people have had no problem. Again, first convert to newstyle 
classes in your 2.x code. This rarely makes a difference, but perhaps 
you so something in the ... part where is does.

 > I despair of ever moving to 3.2

Don't there must be something simple but not now obvious.

> Please reply directly.

Done, in addition to public posting. Please reply to the public posting 
so others can learn also.

-- 
Terry Jan Reedy




More information about the Python-list mailing list