Python is wierd!

Jonathan theebh at yahoo.com
Tue Jul 25 09:55:33 EDT 2000


In article <slrn8nqo34.1ec.scarblac-spamtrap at flits104-37.flits.rug.nl>,
  scarblac-rt at pino.selwerd.nl wrote:
> Python isn't weird - it's much more consistent than you think :-)
>
> Jonathan wrote in comp.lang.python:
> > 1. There are no keywords to declare static or instance variables; it
> > all depends where they are placed(whether it's right after the
<class>
> > statement, or inside a <def>).
>
> This isn't really true. A class variable is declared directly inside
> the class definition, or outside of it if you use the name of the
class
> (to set variable x in class Example, use Example.x = 3).
>
> Instance variables are *always* made by saying which instance it is,
> ie self.x = 3.
>
> Example:
>
> class Example:
>    y = 3 # Directly in the class def, so a class variable
>
>    def __init__(self, x):
>       self.x = x      # Set the instance variable - need to
use "self."
> 	  y = 3           # Just a local variable, neither class nor
instance
>
> 	  Example.y = 4   # Sets the class variable
> 	  self.__class__.y = 4 # Actually works better, because of
inheritance and
> 	                  # some other things. This looks up the
instance's class
> 					  # in the instance, and sets
the class variable.
>    z = 3 # Another class variable, no need to place them right after
the
>          # class statement
>
> So unless it's directly in the class definition, you always need to
say
> explicitly which one you want to set.
>
> > Isn't it harder for other programmers to
> > know at one glance what type of attributes the class define?
>
> As long as you define all of them directly in the class definition
> (and not *only* through a method) you can see it at a glance. And you
> put it in your documentation, of course (doc string).
>
> > 2. No *formal* declaration of static class methods, e.g. no 'static'
> > keyword (though i'm not sure how useful static methods are in OOP
> > design). I read somewhere in this newsgroup that the workaround way
is
> > to define the method outside of the class - doesn't it break
> > the 'encapsulation' a class suppose to have?
>
> Maybe, but as you notice yourself, you don't know how useful these
static
> methods are - if they aren't bound directly to an instance of the
class,
> maybe they shouldn't be in the class in the first place? There are
probably
> counter examples, but I can't think of one right away.
>
> > 3. No keywords to differentiate between passing arguments by
reference
> > or value! An integer is passed by value(reference to a new copy),
while
> > a mutable object is by reference. To pass a list by 'value', i know
you
> > just need to insert one more line(list = [:]), but it seems
> > so 'inconsistent' compared to Java or even PHP.
>
> No no no - *everything* is passed by reference, including that
integer.
> It's just that you can't change integers (they are immutable - why
would
> you want to change the integer 4?), so that if you assign to such a
variable,
> it refers to a new integer (actually, *assignment* is always by
reference
> as well - that's different from other languages).
>
> Try this:
>
> x = 4
>
> def test(y):
>    if x is y:
>       print "x is the same reference as y!"
>    y = 3
>    if not (x is y):
>       print "but now they're a different reference!"
>
> test(x)
>
> ("is" checks for reference identity).
>
Thanks for your posting. i feel much enlightened now:) I guess it's the
fact that arguments(and assignments) are passed by reference in python,
(which is not the default in the languages that i learned so far), and
the 'flexibility' of declaring OOP constructs that cause
the 'discomfort' in me.

Anyway, i just parted with some of my hard-earned money to get a copy
of EPR(Essential Python Ref) thru an online bookstore. Hope it will be
useful!:)

regards,
jonathan


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list