Python Forum
Not having to rewrite 'obj.' on each line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not having to rewrite 'obj.' on each line
#1
Let's say I have this class:
class MyNewClass:
    def func1(self):
		print('something to do')
    def func2(self):
		print('other thin to do')
The normal syntax to call methods of this class is:
obj = MyNewClass()
obj.func1()
obj.func2()
I would like to do exactly the same thing but without having to rewrite 'obj.' on each line as in this imaginary syntax:
obj = MyNewClass()
using obj:
    func1()
    func2()
Does Python allow you to write this kind of code?
Reply
#2
No. Even using a context manager, you'd just be substituting. Obviously you could create a function that could take a class/instance and call methods for that. Examples on SO.

Are you trying to save on typing?
I am trying to help you, really, even if it doesn't always seem that way
Reply
#3
You can do something terrible like this
from copy import copy

class Greeter:
    def __init__(self, to):
        self.to = to

    def hello(self):
        return "Hello, {}!".format(to)

class Magic:
    def __init__(self, instance, enclosing_locals):
        self.instance = instance
        self.enclosing_locals = enclosing_locals
        self.origin_enclosing_locals = copy(enclosing_locals)

    def __enter__(self):
        for key in dir(self.instance):
            self.enclosing_locals[key] = getattr(self.instance, key)

    def __exit__(self, type, value, traceback):
        for key in list(self.enclosing_locals.keys()):
            del self.enclosing_locals[key]

        self.enclosing_locals.update(self.origin_enclosing_locals)

with Magic(Greeter("world"), locals()):
    print(hello())
I bet there's a way to not require the locals() call but I couldn't figure it out in a few minutes' Googling. Note that I'm messing with scopes such that any new variables defined within the with-block which would normally be available after the block, won't be. Anyone familiar with Python reading your code would be confused unnecessarily.
Reply
#4
you can also keep you class in a separate file, then if there's a method that you use a lot, import like:
from class import method.
now you can use method by itself, without the class name.
Reply
#5
(Sep-30-2018, 01:14 AM)Larz60+ Wrote: you can also keep you class in a separate file, then if there's a method that you use a lot, import like: from class import method. now you can use method by itself, without the class name.
That's a good idea. I believe that's how some stateful functions work in the random module. That said, you'd be limiting yourself to singletons (or plural-tons, but a fixed set of them). If that sufficed, I'd say it's an order of magnitude less terrible than what I posted.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Optmized way to rewrite this very slow code liva28 0 1,471 Jul-18-2021, 12:16 PM
Last Post: liva28
  Cleaner way to rewrite fakka 5 3,008 Dec-05-2019, 04:53 AM
Last Post: stullis
  How do I rewrite this .bat file code onto Python? SteampunkMaverick12 4 2,784 Nov-02-2019, 11:28 PM
Last Post: snippsat
  Rewrite a function to make it work with 'bottle-pymysql' nikos 1 1,934 Feb-26-2019, 02:59 PM
Last Post: nikos
  piexif rewrite exif data yawstick 2 3,272 Oct-09-2018, 08:56 PM
Last Post: yawstick
  How to rewrite image file name based on ocr data.txt kevinchr 0 3,621 Apr-16-2018, 07:09 PM
Last Post: kevinchr
  how do i rewrite this code to give me 10 outputs BlackPimpernel 2 2,627 Mar-29-2018, 11:29 AM
Last Post: BlackPimpernel
  Rewrite variable every n times andrea0913 0 2,573 Aug-20-2017, 05:54 PM
Last Post: andrea0913

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020