overriding len in __init__.py - python

  • Last Update :
  • Techknowledgy :

What you can do, is override the length method only on certain classes (this might make a lot of sense for you). For this you can use operator overloading, just override the method __len__ in your class:

class MyList(object):
   def __len__(self, ):
   # Do your thing

When you try to load a name that is not defined as a module-level global or a function local, Python looks it up in the __builtin__(builtins in Python 3) module. In both versions of Python, this module is also availabe as __builtins__ in the global scope. You can modify this module and this will affect not only your code but any python code anywhere that runs after your code runs!!

import __builtin__ as builtins #
import builtins in python 3
llen = len
builtins.len = lambda a: llen(a) - 1

Suggestion : 2

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today. ,Kenneth Love I know I'm missing something trivial here... what is it? Your overriding the len to be incorrect, but the Challenge suggests using super(), which to me, means using the correct len for list and then changing it,Lastly, remove the = from your return statement, you are not setting the value of real_length, just returning a value. return real_length + 3,The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

class Liar(list):
   def __len__(self, items):
   real_length = super().__len__(items)
return real_length += 3
class Liar(list):

   def __len__(self):
   return super(Liar, self).__len__() + 2
def __len__(self):

   # and

real_length = super().__len__()
super().__len__(self)
class Liar(list):
   def __len__(self):
   return super().__len__() + 1
class Liar(list):
   def __init__(self, * args, ** kwargs):
   super().__init__()

def __len__(self):
   return self.__len__()

Suggestion : 3

When an instance method object is created by retrieving a user-defined function object from a class via one of its instances, its __self__ attribute is the instance, and the method object is said to be bound. The new method’s __func__ attribute is the original function object.,When an instance method object is created by retrieving a class method object from a class or instance, its __self__ attribute is the class itself, and its __func__ attribute is the function object underlying the class method.,User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object or a class method object.,An instance method object combines a class, a class instance and any callable object (normally a user-defined function).

def __hash__(self):
   return hash((self.name, self.nick, self.color))
import sys
from types
import ModuleType

class VerboseModule(ModuleType):
   def __repr__(self):
   return f 'Verbose {self.__name__}'

def __setattr__(self, attr, value):
   print(f 'Setting {attr}...')
super().__setattr__(attr, value)

sys.modules[__name__].__class__ = VerboseModule
class Philosopher:
   def __init_subclass__(cls, /, default_name, **kwargs):
      super().__init_subclass__( ** kwargs) cls.default_name = default_name

      class AustralianPhilosopher(Philosopher, default_name = "Bruce"):
      pass
class A:
   x = C() # Automatically calls: x.__set_name__(A, 'x')
class A:
   pass

c = C()
A.x = c # The hook is not called
c.__set_name__(A, 'x') # Manually invoke the hook
class Meta(type):
   pass

class MyClass(metaclass = Meta):
   pass

class MySubclass(MyClass):
   pass

Suggestion : 4

By Bernd Klein. Last modified: 01 Feb 2022.

4 + 5
9
3.8 + 9
12.8
"Peter" + " " + "Pan"
'Peter Pan'