LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python coming from Java/C++: Classes from 1000 feet, help (https://www.linuxquestions.org/questions/programming-9/python-coming-from-java-c-classes-from-1000-feet-help-215206/)

johnMG 08-08-2004 11:50 PM

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"?

_ _ _ _4.a. Are all methods attributes?

5. Do I *have* to define an __init__ method? Why?

Thanks!

Strike 08-09-2004 04:56 PM

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.

johnMG 08-09-2004 08:21 PM

Thanks Strike!

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?

If so, then

Code:

foo = A()
foo.y = 2  # instance variable / bound 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... (?)

Strike 08-10-2004 10:57 AM

Quote:

Originally posted by johnMG
Thanks Strike!

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?

If so, then

Code:

foo = A()
foo.y = 2  # instance variable / bound 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... (?)

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

Sorry, should have been a bit clearer about that :)

p-static 08-10-2004 11:55 AM

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.

johnMG 08-10-2004 08:40 PM

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! :)

Thanks for the replies.


All times are GMT -5. The time now is 05:28 PM.