I'm refactoring code and wonder if this is possible.
def a(array):
class MyClass(object):
def length():
return len(array)
return MyClass
print(a([1,2,3,4]).length())
print(a([1,2,3,4]))
It returns
4
<class '__main__.a.<locals>.MyClass'>
and i want:
4
4
Trying hard, i read about metaclass and __new__ but cant figure out how to do it.
or
class a():
def __init__(self, array):
self.array = array
def length(self):
return len(self.array)
print(a([1,2,3,4]).length()) # 4
print(a([1,2,3,4])) # <__main__.a object at 0x7fab704c9e50>
It's not working, it has 50% functionality.
So sad i don't have time right now but with my today's knowledge its impossible to do it with metaclass. In sqlite3 we can use cur.execute("SQL;"), cur.execute("SQL;").fetchone() so it is possible to do it.
class Metaclass(type):
def __new__(metacls, name, bases, attrs):
try:
return attrs['length']()
except KeyError:
pass
return type.__new__(metacls, name, bases, attrs)
def a(array):
class MyClass(metaclass=Metaclass):
def length():
return len(array)
return MyClass
print(a([1,2,3,4]).length()) # <- This is not working.
print(a([1,2,3,4])) # 4
I would like to hear if it could be done better or smarter tham my solution, because using function returning class it's a hack in my opinion.
Studied this code and its impossible to know when to replace returning object, i will retype this to class without function. Just checked same problem with just single class. Had trouble with passing arguments from method to metaclass.