-1

I want create inheritor (descadent) of python's class time. I use solving from next script, but I obtain message cannot create "builtin_function_or method" instance. Where is problem,please?

from time import time as time
import _classA

class UserTime(time):
    def __init__(self):
        pass
jasan
  • 209
  • 4
  • 15
  • Possible duplicate of [Error when calling the metaclass bases: function() argument 1 must be code, not str](https://stackoverflow.com/questions/2231427/error-when-calling-the-metaclass-bases-function-argument-1-must-be-code-not) – ezdazuzena Dec 07 '17 at 09:42

3 Answers3

2

time.time is a function not a class. You cannot inherit from a function. In the module time struct_time is defined as a class.

from time import struct_time as time

class MyTime(time) :
    def __init__(self, time_as_sequence) :
        time.__init__(self, time_as_sequence)

        # Do what you like
Sven-Eric Krüger
  • 1,277
  • 12
  • 19
0

Based on the Python docs for time.time, the time that you've imported there is a function, not a class, which is why you're getting that error message.

datetime.time may be what you need instead.

bouteillebleu
  • 2,456
  • 23
  • 32
0

Actually what you're trying to do is ILLIGAL :) Anyway, you're trying to subclass a method of the class time which you can't do. Here's a little explanation why:

>>> import time
>>> type(time.time)
<class 'builtin_function_or_method'>
>>> type(time)
<class 'module'>

From what you can see above the time.time is a builtin function or method of a class (in this case, the class "time"), even if you cast a type on "time" itself, would result a class of type "module", which you cannot subclass aswell. But, you could do something that i don't reccomend since it is useless (in my opinion):

import time
class A(type(time)):
    pass

and it would magically be a class of type "type", which is what you want and answers to your question.

>>> type(A)
<class 'type'>

But you have to deal with it in someway, so my question is, What are you actually trying to accomplish? Do you really need to do sort of magic behind the scene to something that could be done in different ways (easily)?

Paradisee
  • 29
  • 8