Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

N00b Python question

Go to solution Solved by fizzlesticks,

object.bar is just a normal object. It has a __call__ member function that gets invoked when you call object.bar(). 

 

So object.bar() calls the object.bar object.

 

Libraries like pyqt need the reference to the object so they are able to invoke it whenever they need to. If you try passing object.bar() to set a listener your giving qt whatever the bar() method returns instead of the bar object itself.

Okay, I have a Python newbie question if someone can answer. Let's say I have a class which has one member method like so

class foo:
     
   def bar(self):
      print('bar method invoke')

      

And I call the function like so. 

object = foo()
object.bar()

My question: what is the difference between that and this

object.bar

Difference is subtle. One has parenthesis, one does not but sometimes if I code GUI in Python(typically pyqt5), and set a listener for a button using a lambda function, foo.bar() will give me error while foo.bar would work. Can anyone give me some answers.  Thanks!

 

Edit: I corrected my code snippet. Sorry if it confused you. I was typing this on a phone and spell check and tiny keyboard were really hindering my typing accuracy. 

Sudo make me a sandwich 

 

Check out my guide on creating your own private cloud storage

 

Link to post
Share on other sites

object.bar is just a normal object. It has a __call__ member function that gets invoked when you call object.bar(). 

 

So object.bar() calls the object.bar object.

 

Libraries like pyqt need the reference to the object so they are able to invoke it whenever they need to. If you try passing object.bar() to set a listener your giving qt whatever the bar() method returns instead of the bar object itself.

1474412270.2748842

Link to post
Share on other sites

I see. I suspected it has something to do with the return values. I do remember if no return statement is specify, a python function would return none. One strange feature in Python is that function itself can be pass around like objects which I am quite uncustom to. 

Sudo make me a sandwich 

 

Check out my guide on creating your own private cloud storage

 

Link to post
Share on other sites
3 hours ago, wasab said:

I see. I suspected it has something to do with the return values. I do remember if no return statement is specify, a python function would return none. One strange feature in Python is that function itself can be pass around like objects which I am quite uncustom to. 

That's not strange it's amazing. You can pass functions are arguments to other functions and then invoke them.

 

This allows you to build up bigger functions from smaller ones and go full function style.

 

It also allows you to return a function based on an a input.

 

I do the same thing at work but in node.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites
3 minutes ago, vorticalbox said:

That's not strange it's amazing. You can pass functions are arguments to other functions and then invoke them.

 

This allows you to build up bigger functions from smaller ones and go full function style.

 

It also allows you to return a function based on an a input.

 

I do the same thing at work but in node.

nah, i like OOP better cuz things get abstracted so fewer details and headaches for me. 

Sudo make me a sandwich 

 

Check out my guide on creating your own private cloud storage

 

Link to post
Share on other sites
Just now, wasab said:

nah, i like OOP better cuz things get abstracted so fewer details and headaches for me. 

Function programming actually gives fewer headaches, data is immutable and functions can't have side effects.

 

I know for sure that the same input will always give the same out put.

 

Though they both have their place, pop can be nice if you need the same base functions and then extend that class later.

 

As with all things in programming they are tools and you use the right one for the job.

 

It's always worth learning different ways of programming.

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites
15 minutes ago, vorticalbox said:

Function programming actually gives fewer headaches, data is immutable and functions can't have side effects.

 

I know for sure that the same input will always give the same out put.

 

Though they both have their place, pop can be nice if you need the same base functions and then extend that class later.

 

As with all things in programming they are tools and you use the right one for the job.

 

It's always worth learning different ways of programming.

 

but you are missing out on powerful features of OOP like design patterns. 

Sudo make me a sandwich 

 

Check out my guide on creating your own private cloud storage

 

Link to post
Share on other sites
30 minutes ago, wasab said:

but you are missing out on powerful features of OOP like design patterns. 

What language do you usually use? Pretty much every popular language has some kind of function pointer like thing that works just like this.

1474412270.2748842

Link to post
Share on other sites
3 minutes ago, fizzlesticks said:

What language do you usually use? Pretty much every popular language has some kind of function pointer like thing that works just like this.

Java normally which doesn't really have anything meaniful until lambda is introduce in java 8

Sudo make me a sandwich 

 

Check out my guide on creating your own private cloud storage

 

Link to post
Share on other sites
9 minutes ago, wasab said:

Java normally which doesn't really have anything meaniful until lambda is introduce in java 8

Check out java.util.function, they have ways of doing the same thing. There's not really anything "non OOP" about storing then calling a function pointer. 

1474412270.2748842

Link to post
Share on other sites
6 minutes ago, fizzlesticks said:

Check out java.util.function, they have ways of doing the same thing. There's not really anything "non OOP" about storing then calling a function pointer. 

Which is something in java 8. Prior to that, passing in function isn't excatly a feature of the langauge and I doubt it is widly adopted by programmers as of now. The thing about oop oriented langauge like java is that everything is an object/class by default. It is simply not possible to do everything functional style.  

Sudo make me a sandwich 

 

Check out my guide on creating your own private cloud storage

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×