Preferences

How is pythons object model less "logically consistent"? Multiple inheritance is dangerous but I'm not sure if its logically inconsistent. And it doesnt have the issue that ruby has of procs not being objects

Ruby borrows a lot from Smalltalk. Everything is an object, including things that are normally keywords elsewhere like Class or Module. There are no public properties on objects, only methods, which is technically only « messages » and object can receive.

Ruby does well with metaprogramming magic because most of the languages own keywords and concepts can be rewritten at runtime. They’re implemented AS Ruby code.

I would say it’s logically consistent because there isn’t as many « special » things. Python’s def keyword is special. You can’t make your own, nor can you hook into it. In Ruby, that’s just a message being passed to the current scope’s object. You can do anything you want to it. Ruby plays by a pretty consistent rule book.

In python everything is an object.

I'm sure there are minor exceptions or arguable cases but that's also the case with Ruby. Most infamously with Proc.

Classes are objects in python, usually instances of class called type or a subclass of type(usefulfor controlling class creation), this is called the metaclass. A constructor call MyObject() is basically just the __call__ method of the metaclass.

Modules are also objects usually of type types.ModuleClass or a subclass although really you can stick any object in sys.modules dictionary and have it act like a module and import it.

Sure it doesn't use messages instead attributes whether field or methods both go through the same lookup method __getattribute__ which goes up the class inheritance chain in an OO manner. There's also __getattr__ for more method_missing like access instead of overriding every object attribute access.

A function or lambda is an instance of function class although I don't think you can't modify or override the function class I think you can hook it to log it.

You can also do all sorts of stuff by hooking module import and rewriting the ast. Or more easily you can decorate/wrap the functions in another function object.

It's true that you can't modify c based classes but you can do a lot of magic in python, people usually just don't because unnecessary magic is frowned on. But sometimes it's useful like the way pytest rewrites assert a==[1,2,3] to print a nice error message with both variables https://github.com/pytest-dev/pytest/blob/main/src/_pytest/a...

I forgot to mention

There's was recently a cool article on things to you can do with python decorators

https://www.hackerneue.com/item?id=42918846

Basically the python function is created as a regular python function but then passed to a jit decorator

@something.jit def some_func(): ....

Which takes the source code and uses it as a dsl for something else compiles it say something to run in the GPU and assigns it the name of the original function. Technically you haven't hooked creation of of the func just replaced it with a wrapper or in this case a related function that doesn't call the original but it does the job.

This item has no comments currently.

Keyboard Shortcuts

Story Lists

j
Next story
k
Previous story
Shift+j
Last story
Shift+k
First story
o Enter
Go to story URL
c
Go to comments
u
Go to author

Navigation

Shift+t
Go to top stories
Shift+n
Go to new stories
Shift+b
Go to best stories
Shift+a
Go to Ask HN
Shift+s
Go to Show HN

Miscellaneous

?
Show this modal