[Python-ideas] Re: CPP Namespaces For Python

2020-10-07 Thread Christopher Barker
On Wed, Oct 7, 2020 at 12:06 AM Random832 wrote: > I think a metaclass [well, "pseudo-metaclass", to use a term I made up for > a metaclass that is not a subclass of type and/or does not return an > instance of type] would be better in this case because the resulting object > should not be a

[Python-ideas] Re: CPP Namespaces For Python

2020-10-07 Thread Random832
On Tue, Oct 6, 2020, at 23:13, Guido van Rossum wrote: > A. New syntax is way too high a bar for a questionable feature. Classes > full of static or class methods were a pattern at my last employer and > it was unpleasant to work with. (Others at the company agreed but it > was too late to

[Python-ideas] Re: CPP Namespaces For Python

2020-10-07 Thread Greg Ewing
New improved version: def submodule(f): co = f.__code__ i = len(co.co_consts) b = bytes([0x64, i, 0x83, 0x0, 0x53, 0x0]) f.__code__ = co.replace( co_consts = co.co_consts + (locals,), co_code = co.co_code[:-4] + b ) return type(f.__name__, (), f()) @submodule def Stuff():

[Python-ideas] Re: CPP Namespaces For Python

2020-10-07 Thread Christopher Barker
I have no idea if this is a good idea, but Python already has modules to be namespaces for a collection of functions and values. And while a class decorator is *supposed* to return a class, it can, in fact, return anything. So you can make a decorator that converts a class definition to a module

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Greg Ewing
On 7/10/20 2:45 pm, Random832 wrote: I think the feature should allow for the functions to directly access each other from the namespace's scope without requiring an attribute lookup. That got me thinking, and I came up with this: def submodule(f): return type(f.__name__, (), f())

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Marco Sulla
On Tue, 6 Oct 2020 at 15:33, Alperen Keleş wrote: > Cars have different states, MovingCar, IdleCar, ParkingCar... Well, IMHO the solution is quite more simple: class Car: def __init__(self): self.state = "parking" def move(self): if self.state != "moving":

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Greg Ewing
In Python 3 you can do this without any decorators. The following works and produces the same output: class AA: def greet(A_instance): print("Hello", A_instance.name) class BB: def greet(A_instance): print("Hi", A_instance.name) class A: def __init__(self, state, name):

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Guido van Rossum
On Tue, Oct 6, 2020 at 6:47 PM Random832 wrote: > On Tue, Oct 6, 2020, at 02:50, Alperen Keleş wrote: > > Please pardon me if my idea is not making sense or already exists, I'm > > kind of new to developing in Python but I had this idea today and I > > wanted to share it with you. > > > > I

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Ricky Teachey
On Tue, Oct 6, 2020 at 9:49 PM Random832 wrote: > On Tue, Oct 6, 2020, at 02:50, Alperen Keleş wrote: > > I think a class type such as "@functionclass" may be helpful for > > creating functions intended to keep a list of methods in a scope. > > > > At the moment, I achieved this via writing

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Random832
On Tue, Oct 6, 2020, at 02:50, Alperen Keleş wrote: > Hi, > > Please pardon me if my idea is not making sense or already exists, I'm > kind of new to developing in Python but I had this idea today and I > wanted to share it with you. > > I think a class type such as "@functionclass" may be

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Christopher Barker
NOTE 1: After writing this whole post, I realized that while you used @classmethod, what you seem to really want is a fully @staticmethod class -- certainly your example was a static method, and some other posters were talking about static method (i.e. "it's bad style to have unused parameters in

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Eric V. Smith
I was just working on that, although I prefer staticmethod: def allstatic(cls):     for key, value in cls.__dict__.items():     if not key.startswith('__'):     setattr(cls, key, staticmethod(value))     return cls @allstatic class C:     def foo(a, b):     print(f'{a}, {b}')

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Guido van Rossum
I think the OP would be happy with a decorator they can just copy-paste. All it needs to do is go over the class dict and apply @classmethod to every “normal” function. Probably skip under names. On Tue, Oct 6, 2020 at 06:46 Ricky Teachey wrote: > cf. this relatively recent conversation on the

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Ricky Teachey
cf. this relatively recent conversation on the same topic-- worth reading in entirety: https://mail.python.org/archives/list/python-ideas@python.org/thread/TAVHEKDZVYKJUGZKWSVZVAOGBPLZVKQG/ As I said in that conversation, in the past I have wanted to have module-like namespaces inside of modules

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Alperen Keleş
Hi Irit, In my case, the code structure is as below. I'm writing a city traffic simulator which includes roads and cars. Cars have different states, MovingCar, IdleCar, ParkingCar... A car can move between different states and it keeps the same information. My solution to this was, Having a

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Henk-Jaap Wagenaar
I cannot answer for Alperen, but I commonly encounter this when writing testing code: generally I use the format: some_module.py tests/test_some_module.py where it is expected the filename to test a module is "test_module_name.py". However, within that, I might want to namespace based on the

[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Irit Katriel via Python-ideas
Hi Alperen,  Why do you need a class at all rather than just a module with some functions? Irit On Tuesday, October 6, 2020, 01:38:21 PM GMT+1, Alperen Keleş wrote: Hi, Please pardon me if my idea is not making sense or already exists, I'm kind of new to developing in Python but I had