Notifications
Clear all

how to code python

80 Posts
9 Users
10 Likes
20.6 K Views
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@pugwash

Posted by: @pugwash

I see where you are coming from, but my question is "Do they NEED to know that they are calling an object method with a parameter?" before they have learnt basic program control!

I'm not advocating they learn one before the other, but rather they have no choice but to learn them in parallel anyway, how can you not?

True, one of the first things you will do is write to the serial monitor, but just as true is that the inquisitive mind will ask, what is this declaration (Serial.begin(9600); actually doing?  Isn't that a part of the learning, and you're learning OOP without knowing it?

Posted by: @pugwash

The more complexity thrown at the beginner, the more off putting it will be.

True, but I don't see anything complex here... do you care to elaborate on what you think is complex?


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@frogandtoad

True, one of the first things you will do is write to the serial monitor, but just as true is that the inquisitive mind will ask, what is this declaration (Serial.begin(9600); actually doing?  Isn't that a part of the learning, and you're learning OOP without knowing it?

No, you are using OOP without knowing it! Learning to use OOP is something quite different!

True, but I don't see anything complex here... do you care to elaborate on what you think is complex?

Complexity for me is relative to where I happen to be on the learning curve!


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@byron

Posted by: @byron

In the Python world there is some debate amongst proper Python experts

I've got news for you... there are flame wars in every language! 🙂

Posted by: @byron

Much easier to read the code without that class self.xxx stuff.

Ahha!  This is where the crux of the issue is, and I again have to disagree with you on this one!
Is thousands of lines of code with repeated code everywhere (IOW, is procedural spaghetti code), easier to read that a well structured object oriented piece of code?

Posted by: @byron

And from the zen of Python 'Simple is better than complex' and 'Readability counts'  I couldn't agree more.

As for the ++ in C.. consider it a C- for me.

As for readability, I think Python is not as intuitive as C++, as it's curly braces clearly define the program components scope, whereas Python can easily loose you with inconsistent indentation!

Anyway, each to their own... C++ is my favorite language by far 🙂


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@pugwash

Posted by: @pugwash

No, you are using OOP without knowing it! Learning to use OOP is something quite different!

Isn't using something, a learning experience?

Posted by: @pugwash

Complexity for me is relative to where I happen to be on the learning curve!

Indeed, complexity is relative, but not everyone is on the same learning curve, right?


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@frogandtoad

Isn't using something, a learning experience?

Not alone! Parrots can learn to talk, but they don't understand what they are saying!


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@pugwash

If you don't use it, you loose it! 😀


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1112
 
Posted by: @frogandtoad

Is thousands of lines of code with repeated code everywhere (IOW, is procedural spaghetti code), easier to read that a well structured object oriented piece of code?

Now if you replaced well structured code using functions I would agree.  No need to repeat code at all.  Functional programming rules! ?    


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1112
 
Posted by: @frogandtoad

The functional version would be

I think you may be loosing sight of the concept of a beginner starting to learn to code.  What I was meaning was that just using functions  rather than classes  is  all one needs for programming microcontrollers, especially for beginners.  Even for more advanced folk, for small microcontroller based projects straying into OOP would  probably not be productive and especially learning oop just for relatively small microcontoller based projects would not be worthwhile imho.  

OK you can do it and clearly love your oop and inheritance but my impression of code even in the commercial world, whilst using classes, has not made much use of inheritance.  I know citations are required for such bold statements but I'm not going to find links to stuff I have read right now (no time right now plus also I'll warrant you can find links to suggest the opposite).

A version of the code you present without classes would be that your class is written as a function.   Of course you cant do inheritance from this functions, but why would you.  Just look at the horrible code you presented ? (eye of beholder and all that).  Ok, you have a spiffing function that does all that you need for controlling a servo motor and you save it in a file for easy reuse in another project.  This new project is unlikely to be using the servos in the same way as the previous project, not least at the GPIO pins or and the physical wiring will probably be different.  That aside lets assume your function is almost useable for your new project but it needs a bit extra.  If this extra will generically improve your function, then amend your function.  If the extra is just for this project then copy the function and amend it in the new projects files.  Make it simple!   Keep it easy to read, understand and follow.  All that inheritance malarky is just horrible.

 

 


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@byron @frogandtoad

Posted by: @byron

eye of beholder and all that

So you dared waking me up ! Now behold a real Beholder !

image

May I just chime in the following ? 

  • OOP's "encapsulation + extensibility" is vital for libraries that have to cater for unknown use cases
  • it can tremendously help in a large project and definitely fosters code re-usability
  • it can be overkill for a small 1-shot project

Eric


   
frogandtoad reacted
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1112
 

@zeferby

Yes, large projects are a different ball game.  But even then... Here is a view about using classes that chimes with me.

Its only one point of view and you gents may not agree.  If you'r a programming wiz and prefer to use classes I'm not trying to make any argument to the contrary.  Do remember, especially for the Arduino Uno, its easy to run out of memory if you get too complicated.   And whilst in Python I have written some small code for the Rpi using classes, this was when I was learning the language and I soon realised that I was not really gaining anything anything but rather adding a layer of unnecessary baggage over just using functions.   Useful functions can be kept in a separate file for reuse and imported into other projects.   Now if I was cleaver enough to write a generic GUI for python such as wxPython then classes would be utilised.  

So lets look at a real world small project.  For my project I create a servo control function / class.    Say the function has code to control two servo's to make a robot's eyes move - left, right, up, down. (up a bit, left a bit, looking at you babe).  Say you write this as a class.   Then you get another project for a robot that also has moveable eyes, but this has three servos and the extra one can blink some eyelids.  Do you write this new code by inheriting from the original class and including the blinking ability in your new code?   Do you copy the original code and add the ability to control another servo in a new function?   Do you copy the original code but also create a new function just to do eyelid blinks?    If you want to provide your code with classes that have inheritance, I will show you mine that does not.  And if I prefer your code I will, well.. inherit it ? 


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@byron

Posted by: @byron

I think you may be loosing sight of the concept of a beginner starting to learn to code.

I don’t think so, but I do think this thread is going a little pair shaped :-), so let me clarify this misconception and others being implied here before we move on.  Initially, this started out as what was easier to learn:

From the OP: "is python or C++ code harder to learn?"

My view is that given C++ the default Arduino language in use, then you should learn to use it.
This is of course a matter preference to ones personal experiences 🙂

But then you wrote:

"Arduino Code is basically just C and does not usually use any of the ++ bits"

I’m also pretty certain that you agreed, when I provided evidence that C++ was mostly used in Arduino rather than C, hence my view to learn C++.

But then you wrote:

"A beginner would be advised to learn C rather than C++ for getting up to speed with Arduino"

I'd like to remind you that C is an even lower level language than C++, therefore not quite as easy to learn as C++. Would a beginner prefer to work with pointers instead of objects with easily accessible properties and methods? As previously noted, when a beginner uses Serial.begin(9600); to set the baud rate, and then Serial.print("foo"); to write something to the serial monitor, they are using C++ and OOP no less. But ironically, you’re saying that this (one of the most common and basic uses), is too complex for a beginner?

Then you implied that C++ was not well structured compared to functional programming:

"Now if you replaced well structured code using functions I would agree.  No need to repeat code at all.  Functional programming rules! ?"

I provided what I think is a well structured example, which can't be reproduced so easily with functional programming - I enjoy learning, so I'm happy to be proven wrong if you care to provide an example - Then we can judge the ugliness 🙂

Now, lets continue...

Posted by: @byron

What I was meaning was that just using functions  rather than classes  is  all one needs for programming microcontrollers, especially for beginners.

Yes, of course you can! I am not advocating to use classes on everything you do in Arduino, just that you can, and shouldn’t overlook learning how to use them for more advanced projects as you improve your coding skills.

Posted by: @byron

Even for more advanced folk, for small microcontroller based projects straying into OOP would  probably not be productive and especially learning oop just for relatively small microcontoller based projects would not be worthwhile imho.

I think you’re missing the main point… beginners are already using OOP, thus already learning OOP, even for small projects though they may not be aware of it... I never said they should start with advanced topics such as inheritance, etc..., so I don't really understand the resistance, because what they are already doing is learning C++?

Posted by: @byron

OK you can do it and clearly love your oop and inheritance but my impression of code even in the commercial world, whilst using classes, has not made much use of inheritance.

Certainly, I love C++ 🙂
Having said that however, I didn’t say a beginner should dive into learning about inheritance... that was just an example to counter your implied claim that C++ was not well structured code.

Posted by: @byron

Ok, you have a spiffing function that does all that you need for controlling a servo motor and you save it in a file for easy reuse in another project.  This new project is unlikely to be using the servos in the same way as the previous project, not least at the GPIO pins or and the physical wiring will probably be different.  That aside lets assume your function is almost useable for your new project but it needs a bit extra.  If this extra will generically improve your function, then amend your function.  If the extra is just for this project then copy the function and amend it in the new projects files.  Make it simple!   Keep it easy to read, understand and follow.  All that inheritance malarky is just horrible.

Classes are far more powerful and useful than you’re giving them credit for.  Again, the example I provided was just that, an example of extending the functionality of an already existing class, which is a great example for using inheritance, so you need to read between the lines here… if I was extending the functionality of such a class for real, I would provide additional interface methods for setting GPIO's you may want for a new project dynamically, and the same for anything else you need to set… there would be no need to continually update or create new classes.

You most certainly do not create a new (or modify the existing) Arduino Servo class every time you need to use a Servo object, do you?

Cheers!


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@byron

Posted by: @byron

Yes, large projects are a different ball game.  But even then... Here is a view about using classes that chimes with me.

Well, the YT video is specific to not using classes under python, and obviously a pro python forum... what did you expect? 🙂 I’d also say that the examples the host provided was more to do with poor programming skills rather than using classes per se - A pretty poor argument to not use classes in my view.

Posted by: @byron

Do remember, especially for the Arduino Uno, its easy to run out of memory if you get too complicated.

Indeed, you can run out of memory pretty quickly under the Uno, but this isn't really related to using OOP practices - Technically it’s the sum of the data types within the class that make up its size.  Having said that however, if your project grows in complexity as you say, then an Uno is probably not the right choice of controller to use anyway, and using classes would be beneficial in helping to structure your program – There’s no reason a class cannot be thought of as just a better function 😉

Posted by: @byron

 If you want to provide your code with classes that have inheritance, I will show you mine that does not.  And if I prefer your code I will, well.. inherit it ? 

LOL, OK, off the top of my head, maybe something like this:

// Using inheritence to extend an EXISTING object
struct BetterServo : public Servo {
  // add new functionality as shown previously...
 };

// Using composition to model a real world object
struct Android {
  String Model;
  BetterServo LeftEye;
  BetterServo RightEye;

  BetterServo LeftEyeLid;
  BetterServo RightEyeLid;
  // Constructor(s) and functions...
 };

void setup() {
  Android Terminator("T2000", etc...);
Terminator.killSomething();
}

Cheers!


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
 

   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1112
 

@frogandtoad

The discourse may have gone a bit pear, so here is my short form with the original poster in mind.

1 Python is easier to lean than C++ (or C or Arduino C) and is the main language used for the Raspberry Pi

2. Arduino does not use Python but uses a language based on C++ which I will call Arduino C.  The Arduino C is more akin to C the C++ in its coding.  You dont need to know about the OOP (the ++ in C++) to be able to program the Arduino.   A book on C will get you far.   A book on coding specifically for the Arduino will be even better.

Here is a link to a freely downloadable book on C

https://magpi.raspberrypi.org/books/essentials-c-v1

Here is a link to a freely downloadable book on Arduino

https://hackspace.raspberrypi.org/books/arduino

3. There are alternative boards to Arduino that can be programmed in micro-python such as the TinyPico.  These boards can also be programmed with Arduino C if desired.

4 Whilst classes in C++ and Python are a most useful part of the languages, they do not have too much use when programming microcontrollers.   Classes in Python are really easy to use and are often over-used.

So for what its worth that is a summery of my wisdom.

Now I turn to the real life programming challenge of programming robot eyes with servo motors.  Your code snipped is a bit of a cop-out as only the full program code will truly illustrate.  But nevertheless I give my code snippets in python (the easier to lean language) that correspond to your code.

# existing function to control servo eyes

MoveEyes( left, right, up, down):

          # program logic ...

Say I was so impress with this I stored it in a file for re-use in another program.  In the new program I use it as follows

from MySnippets import MoveEyes

But I now want it now to control another servo to blink eyelids.  I could have amended the MoveEyes to MoveEyes( left, right, up, down, blink)  and then imported that.  

I could simply have copied and pasted the whole functions code into my new program and amended it.

Or I could keep MoveEyes as is and simply created another function to do the blinking.

BlinkEyes():

# program logic...

This approach would leave me with a very readable and simple program to follow.  Your code does not encourage me to think that using classes and inheritance is going to be to way to go.   You may say thats because of my simple programming requirement in this instance.  But that is the whole point.  Simple programs for beginners to run on small micro controller or micro computers.


   
ReplyQuote
Page 2 / 6