LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2004, 11:50 PM   #1
johnMG
Member
 
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Rep: Reputation: 32
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!
 
Old 08-09-2004, 04:56 PM   #2
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
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.
 
Old 08-09-2004, 08:21 PM   #3
johnMG
Member
 
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Original Poster
Rep: Reputation: 32
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... (?)
 
Old 08-10-2004, 10:57 AM   #4
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
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
 
Old 08-10-2004, 11:55 AM   #5
p-static
Member
 
Registered: Jul 2004
Distribution: Gentoo
Posts: 101

Rep: Reputation: 15
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.
 
Old 08-10-2004, 08:40 PM   #6
johnMG
Member
 
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Original Poster
Rep: Reputation: 32
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Inner Classes KissDaFeetOfSean Programming 1 07-29-2005 03:50 PM
using custom java classes in perl eastsuse Programming 1 12-02-2004 05:41 PM
compile java classes to binary? ludeKing Programming 2 08-15-2004 09:59 AM
Python 2.3 installed but still coming up 2.2.2 why? ifurita Linux - Software 1 10-10-2003 10:43 PM
importing my own classes in Java vanquisher Programming 9 10-02-2003 03:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration