[Tutor] How to set variables inside a class()
Dominik George
nik at naturalnet.de
Tue Nov 26 11:00:03 CET 2013
Hi Reuben,
> class Animal():
>
> flag = True
> print flag
Are you sure you know what this does ;)? Normally, you initialize all
variables in the constructor.
> test.flag = False
> 1)Inside the Animal class(), How can I set the variable 'flag' to FALSE?
Your qustion is a bit unclear. In your above code, you showed at least
two ways of achieving that - one would be in the place where you have
flag = True in your class definition, and the second is when you do
test.flag = False on the instance.
But remember that classes are schemes; what really counts are the copies
of them called instances.
So I think your question is: "When calling a method in an instance of my
class, how do I make it set the attribute flag to False?"
The simple answer is: In the same way you set the name in the
constructor - self.flag = False .
I suggest you should pay more attention to the repeating patterns you
find when programming Python. I have observed that the answer to your
question was all in your code sample ;). So while I (and others) will
happily help you anyway, in order to improve your learning efforts,
please do the following next time you ask:
- Look at the code you have
- Try to find a pattern that looks similar to what you want to achieve
- Try it ;)
- If it fails or is unclear, report that, your assumptions and the results
so we can clarify it
- If you do not find such a pattern, go ahead and just ask, but give a
statement of what you looked out for :).
Cheers,
Nik
--
<burny> Ein Jabber-Account, sie alle zu finden; ins Dunkel zu treiben
und ewig zu binden; im NaturalNet, wo die Schatten droh'n ;)!
PGP-Fingerprint: 3C9D 54A4 7575 C026 FB17 FD26 B79A 3C16 A0C4 F296
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 905 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/tutor/attachments/20131126/65ed08f4/attachment.sig>
More information about the Tutor
mailing list