Python coming from Java/C++: Classes from 1000 feet, help
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Python coming from Java/C++: Classes from 1000 feet, help
Ok, I'm learning Python and am trying to understand how classes work in Python. I'm coming from a C++/Java background, so if my questions are worded strangely, that should explain why. Here's what I want to know:
0. Does the term "attribute" mean methods *and* ... umm... "data attributes"? (Not sure if I'm asking that correctly.)
1. Are all attributes "public"?
2. Are there "class" attributes as well as "instance" attributes?
3. How do I declare my instance/class variables?
4. Does "data attribute" mean the same thing as "attribute"?
Re: Python coming from Java/C++: Classes from 1000 feet, help
Quote:
Originally posted by johnMG Ok, I'm learning Python and am trying to understand how classes work in Python. I'm coming from a C++/Java background, so if my questions are worded strangely, that should explain why. Here's what I want to know:
Okay, here are my responses:
Quote:
0. Does the term "attribute" mean methods *and* ... umm... "data attributes"? (Not sure if I'm asking that correctly.)
Well, attributes can encompass methods as well, but often it's just referring to "data attributes".
Quote:
1. Are all attributes "public"?
Yes. Though the convention is to prepend a single underscore to make data "protected", and a double underscore to make them fully "private". But anyone can still access them as if they were public.
Quote:
2. Are there "class" attributes as well as "instance" attributes?
Yes, these are called unbound and bound attributes/methods in Python. Things are either bound to an instance or unbound to a particular instance (and rather, belong to the entire class).
Quote:
3. How do I declare my instance/class variables?
Code:
>>> class A:
... x = 1 # class variable
...
>>> foo = A()
>>> foo.y = 2 # instance variable
Quote:
4. Does "data attribute" mean the same thing as "attribute"?
_ _ _ _4.a. Are all methods attributes?
See question 1
Quote:
5. Do I *have* to define an __init__ method? Why?
No. __init__ is the constructor method of your class. If you don't need anything special done (other than the normal object setup stuff, which python does for you), then you don't need to define one. If you do want to initialize some things, then this is where that goes.
Ok. So all attributes are "public", and even though you
can use _oneUnderscore or __twoUnderscores, the language
does not enforce any sort of "protected" or "private"
access privileges. And also:
class methods/fields <--> unbound methods/attributes
instance methods/fields <--> bound methods/attributes
Got it. Though, I still don't get one thing you wrote:
Code:
>>> class A:
... x = 1 # class variable
...
>>> foo = A()
>>> foo.y = 2 # instance variable
So, where you wrote 'x = 1', you're defining an unbound data
attribute?
looks strange to me... how can bound methods of A make use of A.y ahead-of-time (that is, inside bound methods of A,
of course) if you're defining A.y on-the-fly in your program code like that?
You're still using them to store the state of an object, no? It's like you're
inventing them as needed, without regard for the methods that have
to work on them... (?)
looks strange to me... how can bound methods of A make use of A.y ahead-of-time (that is, inside bound methods of A,
of course) if you're defining A.y on-the-fly in your program code like that?
You're still using them to store the state of an object, no? It's like you're
inventing them as needed, without regard for the methods that have
to work on them... (?)
They can't make use of A.y ahead of time, because A.y doesn't exist It's bound simply to that instance, and not to the class at all. You generally won't do this, though. The normal way you see bound variables defined is in the __init__ methods:
Code:
>>> class A:
... def __init__(self):
... self.foo = 1
...
>>> x = A()
>>> x.foo
1
That's one of the most confusing things coming from a compiled language to an interpreted language like python. The variables can be created on the fly at any point, and as long as you make sure that they exist by the time you call the method, it'll work.
Strike wrote: They can't make use of A.y ahead of time, because A.y doesn't exist. :) It's bound simply to that instance, and not to the class at all.
Ah. That's a new concept to me: A bound data attribute -- but instead of one being bound to every object you produce from a given class, it's just a single unique attribute only bound to one specific object. I see what you mean now, and also that it's probably a kinda' less-often-used thing, though still somewhat instructive.
You generally won't do this, though. The normal way you see bound variables defined is in the __init__ methods.
Ahhhhh. Ok. Now we're cookin'. So, if you're careful to write your __init__() method before all others (which I'm guessing is customary), you can have all your bound data attributes ready and waiting for any methods that will later be using them. Sweet.
p-static wrote: The variables can be created on the fly at any point, and as long as you make sure that they exist by the time you call the method, it'll work.
Sounds like regular fly-by-the-seat-of-your-pants programming! :)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.