Notifications
Clear all

how to code python

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

@casey

Posted by: @casey

My understanding is that a class is a little bit like a struct except you can include the functions within the struct that are used to handle the data. 

In C, a structure (using the struct keyword) is known as a POD (Plain Old Data) type, because it does not have the ability to incorporate functions, only variable types.  In C++ however, the C structure (using the keyword struct), behaves exactly like a C++ class, except that it's internal data members and member functions are public by default.

In the example you presented, you could just change the identifier name class with the name struct, and remove the public and or any private specifiers, and it will work just the same.

In your example of breaking it out however, the reason it (the LED(s)) just stays on is because you have broken the association by removing it out of the class.  This is not a real problem when you understand what happened 🙂

When you took the functionality out of the object, you placed it in a function outside of the object, so it doesn't know about it, and the reason for that is because by default, the C and C++ language pass object around by value - This means that a copy of the object is passed to the function, and not the real object you intend the function to be working upon.

The simple fix is to add one character to your code 😉

In the following function, just add an ampersand character: "&", which means the object is now being passed to the function "By Reference", in other words, you are telling C or C++ to not make a copy of your LED object, and perform the operation on the original LED object:

void Update(LED &led) 

Cheers!


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

@byron

OK, let's break this down again, because I'm not sure you understand my comment, or code for that matter.

Posted by: @byron

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

Again, I don't have a problem if you think python is easier than C or C++ - We all have our preferences, and mostly work with what we feel the most comfortable.  True... python is the language for choice for the Raspberry Pi, and C++ is the language of choice for Arduino, which happens to incorporate a common core language of C.

Posted by: @byron

2. Arduino does not use Python but uses a language based on C++

Sorry, but that is clearly wrong, and should read:  "The Arduino uses C++ as it's language, and does not use python".  A book on C will certainly get you far in C, but will teach you ZERO about using member functions which are a mandatory part of using and learning how to program the Arduino - I just can't see how you can dispute that point? - C is a procedural language and does not offer such features!

Posted by: @byron

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.

Indeed there are, and I have no issues with that at all - Like I said previously... use the right tool for the job.  Sure, C++ is my favorite language, but I won't just use it because I like it when there is a better suited language that I can use for whatever the project calls for.

Posted by: @byron

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.

Again, this is a matter of opinion.  I am not forcing anyone to use classes, just promoting awareness of their availability and power, should one like to learn and improve their programming designs under Arduino.

Posted by: @byron

Your code snipped is a bit of a cop-out as only the full program code will truly illustrate. 

Um... I snipped it for brevity, because I had already provided an earlier example to you of how it would look and be used - Again, you really need to read between the lines of what I presented - Did you expect me to write a fully fledged program for you?  If you do not understand what I presented, then just ask and I will be happy to answer any concerns you may have.

Posted by: @byron

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 ...

LOL, my code was a cop-out, and this is easier? ... Like you said earlier... it's in the eye of the beholder!

Posted by: @byron

Simple programs for beginners to run on small micro controller or micro computers.

I have stated a number of time now, that I am not saying beginners should start with advanced C++ topics, so repeating that again will only make me think you do not comprehend the English language.  If you want to program robot's with eye's, then I would also argue you've gone beyond the simple program, and no longer in the realm of the beginner.


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

In the following function, just add an ampersand character: "&", which means the object is now being passed to the function "By Reference", in other words, you are telling C or C++ to not make a copy of your LED object, and perform the operation on the original LED object:

[edit] - Made a mistake there... C does not have references, but can use pointers.
References are a C++ feature!

@dronebot-workshop,

Bill, has the editing time of an existing post been reduced?


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

On the contrary... if you want to learn Arduino, even the most basic libraries use classes with member functions, so learning about OOP and how to pass objects around will make you (IMO), a better Arduino developer. 

I have stated a number of time now, that I am not saying beginners should start with advanced C++ topics

Posted by: @frogandtoad

will only make me think you do not comprehend the English language.

So OOP is not an advanced topic then.  I must learn to comprehend English better ? .   I've given my thoughts  aimed at the original question of learning C++ or Python.  I've also suggested to you that I think the use of OOP is not really for microcontroller hobby programs and classes are ofter overused when its simpler just to use functions.   You disagree and present your case.  Fair enough you have not convinced me but you may have convince others who knows.

Posted by: @frogandtoad

Did you expect me to write a fully fledged program for you

Yes, the function or class to move one servo left or right and another servo up or down is only a matter of a few lines of code, probably less than your original inheritance example and much less than you lengthy postings.   Then we would have a real world example to work with to then extend the function/class to use yet another servo.  This would be better than a contrived class example and mutual cop-outs. (and my cop-out was shorter than yours -  so you could say yours is bigger than mine ? ha ha )


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

@byron

Posted by: @frogandtoad
Posted by: @frogandtoad

Did you expect me to write a fully fledged program for you

Yes, the function or class to move one servo left or right and another servo up or down is only a matter of a few lines of code, probably less than your original inheritance example and much less than you lengthy postings.

Actually my code would be a lot less than yours in a real life program, because I already inherited the functionality I need from the existing Servo object - All I did was give it "additional functionality".

I think you forgot that Servo already has a write function which is now encapsulated as a part of BetterServo, so I can just call it to move left, right, up, down, and any angle I desire.

The BetterServo object can now do everything the original Servo object can do, and has built in awareness (it can count, do things internally based on a move, etc...) on top of it.

Anyway, I doubt we're ever going to agree, but that's fine... go with what you know best and feel most comfortable with!

Happy coding! 🙂


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

Actually my code would be a lot less than yours in a real life program, because I already inherited the functionality I need from the existing Servo object - All I did was give it "additional functionality".

You would be expected to include the code for your original class. No cheating now tish tish?,  My new code took on all the functionality of the existing function with a simple 'import' statement and I gave the new program additional functionality with a new function.   Both your approach and mine work fine, but I was suggesting a more simple and more readable approach was better as I see it for this sort of program without deploying OOP inheritance strategies.

 I can appreciate that an advanced programmer in C++ would not readily change their approach as suggested by a programming dabbler for  beginners and fellow dabblers.   I think you don't wish to play with this any more, so thanks for your interesting input and happy coding to you too. 

 


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 
Posted by: @frogandtoad

The simple fix is to add one character to your code 😉

In the following function, just add an ampersand character: "&", which means the object is now being passed to the function "By Reference", in other words, you are telling C or C++ to not make a copy of your LED object, and perform the operation on the original LED object:

void Update(LED &led) 

Cheers!

That worked. The leds are now flashing as expected.  So I passed a new LED object rather than the address of an already created object?

On the OOP debate you can still use objects like the Serial object without knowing how to write your own classes.  Still it would be foolish not to learn how to write and read a class regardless whether you use them yourself or not.  For the last 10+ years I have only being using FreeBasic so I am a bit rusty on the C++ .  Even when I was using C++ all my short simple programs did not include writing my own classes thus not helping getting a good grasp on their use.

 


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 
Posted by: @casey

Still it would be foolish not to learn how to write and read a class regardless whether you use them yourself or not. 

I fully agree with that.  But the OOP debate as you put it was in the context of a beginner learning C++ to write Arduino code.  I suggested you don't have to learn to use OOP to get going with Arduino.   Classes should be considered an advanced topic that can come later. Also the overuse of classes in small programs in the context of unnecessary complication to the detriment of readably and simplicity was suggested.   But indeed one should be encouraged to go on to learn all about Classes, encapsulation, inheritance, polymorphism and such fun stuff to determine if and when they should be used for oneself.  And once one has learnt how to use classes and objects would one then use this paradigm in one's microcontroller programs?  I think probably not as functions are all that is required for small programs.  But Classes will come in handy when you write the API for your PC based Robot Swarm Location Visualisation  program.  (Did I mention Python is easier to lean than C++ which was what the original poster asked ? )


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

Even when I was using a c++ compiler years ago I didn't feel the need to write classes for the type of programming I was doing.  However I of course had to use them and could see how that made my programs simpler to read.  I also tried to learn OO but found it too much bother to program that way 🙂

My first language was BASIC and Assembler and much much later plain old C so OOP wasn't on the radar.

No I haven't found Python easy to learn.  I would rather use c++.   Unfortunately it doesn't have the support Python has for a hobby programmer.  The indent method I find annoying even worse than squiggly brackets.

Python is a high level language that depends on libraries probably written in C++ to perform time critical things.  One of my interests was processing images and that requires some super fast code best written in a low level language like assembler or c++.  As I wrote earlier I have been using FreeBasic which compiles near to c++ speeds and has an inline assembler and other low level c++ like capabilities to speed up your programs. 

However I am forced to learn Python for some things so I am doing it.  Maybe over time I will get to like it.

As an aside on Python's indent method to block code think how it messes up when the indents are lost.  Is there any simple program to restore them?  With BASIC and C++ indents are for readability and you can run a program to resize or insert indents if they are lost.

You have to learn to write and use classes in Python.

 


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

@casey 

When I started out with python is was on an Rpi with the python 2.7 IDE.  Many a time I got a validation error message with turned out to be an incorrect indent that was not so easy to spot.   But now I code in Visual Studio Code and this helps enormously in keeping my code nicely structured.   There are other nice python editors and plenty of beautifiers too that a google will show.  Here is a link to Python in Visual Studio Code.

https://code.visualstudio.com/docs/languages/python

And when I say python I mean to say use at least python 3.7 to get some of the best developments of the language  (F strings are really nice).  As you say a lot of python libraries are written in C++ and I find that gives power with the ease of program readability.   I don't quite see what you mean when you state that you have to learn to write and use classes in Python.   They are easy to use and learn but as all class type code they have some overhead and readability concerns.  Indeed much of my spiel (and probably too much - sorry folk) in this topic has been about the unnecessary over use of classes where imho using functions would be better.   But no, there is no requirement to lean to use them to use python effectively  though perhaps you do need to do so for whatever you are using python for.

I'm not sure if it was for you in some previous forum post that I recommend the corey schafer youtube videos but I mention these again as they really are a first rate set of tutorials.   He does a set of Python Tutorials for Beginners (1 to 10 I think) which really get you into the meat of Python, but also a lot of more advance tutorials too.   He does a couple of tutorials on multiprocessing and threading, stuff I had not even considered before as being  too advanced for me.  But the ease that  you can do this in Python code was mind boggling to me.   

 


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

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

@frogandtoad

perhaps for a programming philosophy category 

Oh God, please NO!

There is more than enough philosophising on this forum already, which have already challenged various belief systems.

Almost to the point of the programming equivalent of "Religous Wars" ? 


   
frogandtoad reacted
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 

May add to the Holy Wars please?

I was working in the Arduino IDE most of the night racking my brain trying to remember how to properly define an Arduino Class in Tabs.   All I really wanted to was fresh sheet of paper to continue writing my code on because I don't like it when a single tab starts getting too long. 

Anyway, being the lazy dude I am I simply created a header table MyNewClass.h    And I just defined a new class and wrote my code.  Then I #include MyNewClass.h   and made an object from it MyNewClass MNC;

It worked just fine. ? 

I could have gone to bed right then if I was smart.  But not me, I had to be dumb and insist on doing it the right way. ? 

So I moved MyNewClass over to a *.cpp file and then set out to create a new *.h file to describe the class structure.   Unfortunately I had forgotten all the intricate details and it took me the better part of the wee hours of the night to relearn them.   After having made a gazillion mistakes and beating my head against the wall, I FINALLY got it all cleaned up. 

It takes more work to do it this way, plus I end up with two tabs for a class instead of just one.    I also see that a lot of the stuff is redundant.   For example, it really doesn't matter whether you define your variables in the header first before using them in the actual code file.   It's more like they are just there for information so that later if you want to know what's in the actual code you can just look at the header and see it all laid out.   This is most likely just more of programming convention than actual being needed for the code to run.

In any case, I'm all refreshed now on how to do it.   The code for my robot is staring to become complex enough that breaking it up into smaller classes just makes far more sense.    This way if I decide to change this code up a bit I can do it just in the class instead of having to dig around in a huge main program.

Of course I've been using classes with Python, but that's way easier to do.    Also, in C# I learned earlier tonight how to break the code up into "partial classes".    It's all the same class, it's just a way of breaking the single class up into multiple files, or sheets of paper.   I need to keep all my code in tiny tidbits anymore.   When a page starts getting too big my brain starts to hurt. ? 

So for me it's really all about just breaking things up into separate tabs, files, or sheets of paper so-to-speak.

As far as having Holy Wars over which programming language is the best I've chosen to join "Doctors without Boarders". ? 

I use C++, C#, and Python on a regular basis.   And as far as I can see they all have their pros and cons.  Not sure if I even have a favorite.   C# is very compatible with Windows Forms programs.   So that's why I use it there.

DroneBot Workshop Robotics Engineer
James


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

@robo-pi

Doctors without Boarders

Is that about an MD who runs a B&B with no guests? ? 

Must be getting late over there!


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

@casey

Posted by: @casey

No I haven't found Python easy to learn.  I would rather use c++.   Unfortunately it doesn't have the support Python has for a hobby programmer.  The indent method I find annoying even worse than squiggly brackets.

I don't think python is hard to learn, as that's one of the main reasons for it's popularity, but I also hate that indentation scheme, which I actually think makes it harder to read.  I also see that because of its ease, it attracts people from all walks of life who may not necessarily have a great deal of programming knowledge in what I think more important than the language syntax, is actual programming concepts (i.e:- data structures, control flow, decision making, etc...), as these are applicable to any language ever invented.

C++20 official release is due around June/July this year, with a bunch of new features, including features borrowed from python, and if you haven't looked at it features of late, you might be really surprised.  Sadly however, a lot of these new features may never make it into the minimal Arduino libraries, but hopefully one day they may just incorporate some of them.

For advanced developers and interested readers: Coming features for C++20

Cheers!


   
ReplyQuote
Page 3 / 6