About
Swaroop C H is 29 years of age. He is a coder and startupper. He has previously worked at Yahoo!, Adobe, his own startup and Infibeam.
Views
Support
Personal tools
COLLECTION
Collection
Talk:Python en:Object Oriented Programming
From Notes
i miss the instruction that class names should always begin with a captial letter like:
def MyClass():
-Horst --http://www.spielend-programmieren.at 07:13, 30 August 2008 (UTC)
objvar.py
added a print line into the class/poulation example. the line makes it more clear to me that the program ends now and all class instances (the persons) begin dying. Before, i always wondered why the persons die if there is no person.kill() method.
-Horst http://www.spielend-programmieren.at 07:36, 30 August 2008 (UTC)
|
84.114.149.87 said: | On 07:49, 20 October 2008 |
|
i love the robot example. just wondering, is it necessary to put "klass" inside the class-method howmany(klass) ? I would prefer a simple: howmany(): without an argument inside the parantheses, is that possible under python 3.0 ? -Horst horst.jens@spielend-programmieren.at | |
| Reply to 84.114.149.87 | |
|
Swaroop said: | On 17:02, 21 October 2008 |
|
Hey Horst, You are right. I am now using staticmethod instead of classmethod in the example. | |
| Reply to Swaroop | |
|
99.50.135.239 said: | On 05:10, 29 October 2008 |
|
To start I'm new to python and programming in general. so my questions are probably pretty dumb, but this line :: print('Name:"{0}" Age:"{1}"'.format(self.name, self.age), end=" ") :: seems to give python 2.6 some trouble. I know that py3k is not supposed to be back compatible but I'm confused about what this line does. the code works in py3k. but what is end=" "? is it a variable inside of a print function? what is it used for? and why would it give py2.6 trouble? Other than this part Your book has been great. Its easy to read and easy to understand. there have been a few times where I was unsure about what you were talking about but got a good idea from your examples. | |
| Reply to 99.50.135.239 | |
|
99.50.135.239 said: | On 23:34, 29 October 2008 |
| Also I'm not sure if your aware of this but the output for your metaclass demo has a traceback error. printed on the page traceback error I haven't ran the code myself yet. | |
| Reply to 99.50.135.239 | |
|
Swaroop said: | On 01:38, 31 October 2008 |
| Thanks for pointing it out! I have corrected the example. | |
| Reply to Swaroop | |
How r u Swaroop , very nice book ..
When i try the example in "Class And Object Variable" section (objvar.py) and change variable name in code :
droid1 = Robot('R2-D2') droid1.sayHi() Robot.howMany()
to:
p = Robot('R2-D2') p.sayHi() p.howMany()
when i run that , it run with the following error :
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0x00A16990>> ignored
but when i change the variable name back to for example "droid1" it work without error !!
Why that happened !!?
__del__
It is a bad idea to use __del__ to demonstrate class variables. As you stated it is not guaranteed when it is run. But it is also not guaranteed that ist is ever run – even if you use del. It is up to the GC when to delete the object. The situation is getting even worser on interpreter exit (cf. Python documentation).
There should be a bigger warning that __del__ should (must?) not be used for anything vital and that it behaves not like a C++ destructor.
Re: The __init__ method
After spending some time away I still do not understand the explanation for why the object variable needs to be tied to a second variable. My intuition wrongly (I checked in the REPL) assumes that I should be able to Person('james').sayHello('Hello, my name is,' name) However this doesn't work.
Or, maybe the case is that 'name' is only a variable within the namespace of __init__ such that it's value is no longer of consequence once python leaves the __init__ block. Thus I could perhaps tie name to name by doing: name = name, which would be ugly, but it would give me my expected behavior; although if I understand correctly I no longer expect it. ...Nope, that's all wrong.
After playing with IDLE I think the self refers back to the __init__ block, keeping the variable local to the object, instead of global to the entire class ...??? Thus you could write "self.arbitrary_string_goes_here = name".
I'm sorry, either I'm being dumb and not understanding (I'll take no offense if this is the case) or this isn't as clear as it should be. Well, now I understand well enough to get back to it. --71.86.95.244 06:51, 15 May 2009 (UTC)
It is the concept of scopes that needs to be understood (did the book mislead you at some point?) - there is no 'name' variable declared according to that statement because what happens inside Person stays inside. -- Swaroop 18:17, 16 May 2009 (UTC)
- No, but the following italicized language was confusing.
Here, we define the __init__ method as taking a parameter name (along with the usual self). Here, we just create a new field also called name. Notice these are two different variables even though they are both called 'name'. The dotted notation allows us to differentiate between them."
- No, but the following italicized language was confusing.
Re: wrong use of word
The sentence "An analogy is that you can have variables of type int which translates to saying that variables that store integers are variables which are instances (objects) of the int class" does not give an analogy, which is defined as a comparison, say, to something dissimilar. For example, an analogy would be to say that sleep is the downtime of the brain (by comparison to a computer's downtime).