2

could there be any problems, that might occur using class methods directly on created instance like that?:

    TmpClass('lol').printx()

Class example for reference:

    class TmpClass:
        def __init__(self, x):
            self.x = x

        def printx(self):
            print self.x
5
  • 1
    Why not, if that's what you intend to do? If you are not going to use the instance later, then why should you assign it to a variable? I would even say that assigning it to a variable will be misguiding and any descent IDE will highlight the variable as unused. Commented Jan 25, 2016 at 11:49
  • Avoid using something like res = TmpClass('lol').printx() . 'lol' will be printed, but res will be None. Commented Jan 25, 2016 at 11:53
  • @Eli Korvigo , I'm new to python, but trying to improve. Coming from C, I take with caution everything that seems not intuitive to me. In this case, I could not find information online on my own. Commented Jan 25, 2016 at 11:55
  • Can you be more specific about what you mean by "fine"? Are you asking if it is safe or are you asking if it is efficient? Most people here seem be recommending against it because it would be inefficient if it's done in a tight loop. But is that what you are asking about? Commented Jan 25, 2016 at 12:07
  • @Dmitry Rubanovich , I want to know if "I should use such method". I'm going to apply this knowledge in real projects, so my question is from all points of view including code readability. Commented Jan 25, 2016 at 13:52

2 Answers 2

1

If you do MyClass('a').my_method() like that in a loop, for instance, you'll give the garbage collector a lot of work to do.

Here's an example using the ipython conslole

In [11]: class Asdf(object):
   ....:     def __init__(self, x):
   ....:         self.x = x
   ....:     def printx(self):
   ....:         pass
   ....:     

In [12]: def asdf(x):
   ....:     pass
   ....: 

In [13]: %timeit Asdf(9).printx()
The slowest run took 14.89 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 272 ns per loop

In [14]: %timeit asdf(9)
10000000 loops, best of 3: 56.9 ns per loop

As you can see, it takes a lot of time to instantiate and clean up the created objects, compared to just calling a function. Now in some cases just calling a function is not enough/ appropriate. But these are just my 2 cents concerning specifically the performance issue

Worth noting: I excluded the print from the function/method. If I left the print in, then the results look very different:

For the class instantiation, i got 100000 loops, best of 3: 7.42 µs per loop, and for the function call 100000 loops, best of 3: 7.4 µs per loop.

This is just to show that sometimes, little performance improvements bring ridiculously small improvements.

Sign up to request clarification or add additional context in comments.

Comments

1

It depends.

If you want to use this object only once, it's not a problem. Issue would might occur if you would instantiate so many anonymous objects instead of named one, that your program would be inefficient and consuming a lot of memory.

It's the same story like with functions and lambda expressions.

2 Comments

"Anonymous objects," as in, objects without any remaining references to them, are generally cleaned up by the garbage collector, which means they wouldn't consume memory.
Yes, but you still have to create them, which takes time and takes memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.