LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 11-11-2009, 10:48 PM   #1
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Rep: Reputation: 15
PYTHON ! please help me with one little code


Code:
>>> class A(object): attr = 1                                   
>>> class B(A):      pass
>>> class C(A):      attr = 2
>>> class D(B,C):    attr = B.attr
>>> x = D( )
>>> x.attr
1
why it gets the attribute 1 of A? the attribute of B is "pass",and why it goes up to get the attribute 1 of A? it did not say "attr = A.attr"? i am so confused about change from new-style class to classic class!

it is actually a new-style class,it should goes with attribute order by the new-style. why it changes the selection order of an attribute to get the attribute of A by just add "attr = B.attr"?why A? why it goes up to get A? thanks!

Last edited by topheraholic; 11-13-2009 at 09:46 PM.
 
Old 11-11-2009, 11:12 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
WTF?

What language is this? What are we looking at?

Could you please provide a little background to your question? Tells us a little bit about the problem?

TIA .. PSM
 
Old 11-12-2009, 04:28 AM   #3
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by paulsm4 View Post
WTF?

What language is this? What are we looking at?

Could you please provide a little background to your question? Tells us a little bit about the problem?

TIA .. PSM
what do you mean? you can not see what i said? what is WTF? where did you see that?
 
Old 11-12-2009, 04:36 AM   #4
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
What he means is that you haven't given the basic information - which language this is and what it is supposed to do.
 
Old 11-12-2009, 07:40 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by XavierP View Post
which language this is
If you or paulsm4 are actually curious about that, the "find more posts by" link, from the pull down from the OP's name, tends to answer such questions.

topheraholic has been asking Python and shell programming questions. You don't need to know much Python nor shell to guess this question is Python, not shell. (I'm not certain, but pretty confident, that it isn't a third language).

Topheraholic, if you actually wanted help you should have a better subject line for your thread so experts in that topic might read the thread.

If you want help with Python, put Python in the subject line. You can edit the subject line even now by editing your first post in the thread and then clicking the "go advanced" button.

It also wouldn't hurt to give a bit more background info as Pausm4 and XavierP suggested.
 
Old 11-12-2009, 09:47 AM   #6
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Quote:
Originally Posted by johnsfine View Post
If you or paulsm4 are actually curious about that, the "find more posts by" link, from the pull down from the OP's name, tends to answer such questions.

topheraholic has been asking Python and shell programming questions. You don't need to know much Python nor shell to guess this question is Python, not shell. (I'm not certain, but pretty confident, that it isn't a third language).
Good find. Though I would question the need for anyone to have to search a user's history to find out what help they actually need. If anyone comes to someone for help, a basic is to give them the tools/info to do so.

That said, a code snippet (I assume that this is what we're getting) is of limited use, if we know what it should be doing, what it actually is doing and what has been done to it we can then bring out an answer.
 
Old 11-12-2009, 10:13 AM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Sorry about side tracking the thread even further, but...

Quote:
Originally Posted by XavierP View Post
I would question the need for anyone to have to search a user's history to find out what help they actually need.
It's more about some combination of curiosity and helpfulness, certainly not "need .. to search". But I routinely check the OP's other recent posts for insight into various aspects of the question.

People should not ask follow up questions in a new thread, providing neither a link to the previous thread nor background info in the new question. But in fact they do so. There is context in a previous thread often enough that is is worth checking.

People also frequently quote out of context fragments of information from some online source without providing a link nor even mentioning that they are quoting a fragment from an online source. When I guess that has happened (more often in IR forums than at LQ, but sometimes here as well) I google the quote and usually find the same online source, so I then know what they misunderstood as the basis for their question.
 
Old 11-12-2009, 11:19 AM   #8
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Yeah, admittedly I was thinking more in an "ideal world" scenario rather than an "actual world". Internet detectivery should only be applied for fun and/or malice () not to be able to provide help!
 
Old 11-12-2009, 12:00 PM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Wink

Topherholic -

It looks like you had a "WTF moment" when you read my reply. Exactly the point I was trying to make: if you ask a question, you need to put the question in some kind of "context". In this case, we needed to know a) you were asking about Python, b) you were in the Python interpreter, and c) your question was about how Python treats an attribute that's defined in one class appears in different class (one that "uses" the first class).

About the acronyms:
http://www.al6400.com/resources/acronyms.shtml

About the Python question:

Code:
% # Here's another, slightly different example:
% python
  Python 2.4 (#1, Mar 22 2005, 21:42:42)
  [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
  Type "help", "copyright", "credits" or "license" for more information.

>>> # Define some class "A" and use it:
... class A(object) : attr = 1
...
>>> a = A()
>>> a.attr
1

>>> # Now define class "B":
...  class B(object) : attr = 2
...
>>> b = B()
>>> b.attr
2

>>> # Finally, what happens if class "C" is composed of "A" and "B"?
... class C(B,A) : pass
...
>>> c = C()
>>> c.attr
2
'Hope that helps .. PSM

PS:
To paraphrase The Beatles (from "Sgt. Pepper"): always please "indicate precisely what you mean to say..."

Last edited by paulsm4; 11-12-2009 at 12:03 PM.
 
Old 11-13-2009, 10:36 PM   #10
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by paulsm4 View Post
Topherholic -

It looks like you had a "WTF moment" when you read my reply. Exactly the point I was trying to make: if you ask a question, you need to put the question in some kind of "context". In this case, we needed to know a) you were asking about Python, b) you were in the Python interpreter, and c) your question was about how Python treats an attribute that's defined in one class appears in different class (one that "uses" the first class).

About the acronyms:
http://www.al6400.com/resources/acronyms.shtml

About the Python question:

Code:
% # Here's another, slightly different example:
% python
  Python 2.4 (#1, Mar 22 2005, 21:42:42)
  [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
  Type "help", "copyright", "credits" or "license" for more information.

>>> # Define some class "A" and use it:
... class A(object) : attr = 1
...
>>> a = A()
>>> a.attr
1

>>> # Now define class "B":
...  class B(object) : attr = 2
...
>>> b = B()
>>> b.attr
2

>>> # Finally, what happens if class "C" is composed of "A" and "B"?
... class C(B,A) : pass
...
>>> c = C()
>>> c.attr
2
'Hope that helps .. PSM

PS:
To paraphrase The Beatles (from "Sgt. Pepper"): always please "indicate precisely what you mean to say..."
thanks for the suggestions of all your guy!
i know what you have pointed out,but maybe my point is not that simple.

Code:
>>> class A(object): attr = 1                               
>>> class B(A):      pass
>>> class C(A):      attr = 2
>>> class D(B,C):    attr = B.attr
>>> x = D( )
>>> x.attr
1
this is a new-style class,what is confusing me is why it change the search order of an attribute just by add "attr = B.attr"? why it goes up to class A? the original new-style class search order of an attribute is D B C and then A.and now it is D B A , right? why A? the expression "attr = B.attr" is just a sign of changing search order of classic class to new-style, what is the search order if A is the place where classes are mixed together A(A1,A2)? it goes like this"D B A A1 C A2" or "D B A C A1 A2" the "attr = B.attr" is a one-time job or change the entire search order from new-style class to classic class? thanks!
 
  


Reply



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
Error in Perl Code : Bad switch statement(Problem in code block)? near ## line # suyog255 Programming 4 02-20-2008 05:35 PM
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
LXer: Code Craft: The Practice of Writing Excellent Code LXer Syndicated Linux News 0 01-09-2007 04:03 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM

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

All times are GMT -5. The time now is 09:17 AM.

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