translating PHP to Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave

    translating PHP to Python

    Anyone familiar with PHP? I'm trying to make a translation. In PHP you
    can get the current object's name by going like this:

    get_class(item) == 'ClassName'

    I've tried type(item), but since I can't be sure if I'll be in __main__
    or as a child object, I can't guarantee what that value will return, so
    I can't just manipulate that value as a string.

    Is there a simple way to get the current object's name? You would think
    __name__ would work, right? It doesn't.

    Now here's another, similar one:

    You can reference an object's parent object directly in PHP, like so:

    //note the charming use of semi-colon. isn't it cute?
    parent::__const ruct(
    $stuffInAWeirdS yntaxThatDoesnt MeanAnythingWhe nYouReadIt);

    I'd like to avoid passing a reference to an object's parent in
    __init__, but is there a built in way in Python to say "You, Parent
    Object, do ...stuff!"

    Thanks!

  • Farshid Lashkari

    #2
    Re: translating PHP to Python

    > Is there a simple way to get the current object's name? You would think[color=blue]
    > __name__ would work, right? It doesn't.[/color]

    className = item.__class__. __name__
    [color=blue]
    > I'd like to avoid passing a reference to an object's parent in
    > __init__, but is there a built in way in Python to say "You, Parent
    > Object, do ...stuff!"[/color]

    Use the super() function to access an attribute of a parent clas

    class C(B):
    def meth(self, arg):
    super(C, self).meth(arg)

    -Farshid

    Comment

    • Dave

      #3
      Re: translating PHP to Python

      Farshid,

      This is a great help, thanks.

      The second point won't work, though, because by parent class I mean,
      simply, the object that created the current object, *not* the class the
      current class is based on.

      So, for example:

      class A(object):
      def __init__(self):
      self.thing = Thing()
      self.thing.meth ()

      def do_stuff(self):
      print "Stuff"

      class Thing(object):
      def meth(self):
      #now here's what I WANT
      self.parent.do_ stuff(args)

      Is there a built in way to do this in Python, or do I have to pass
      "parent" when I init Thing?

      Sorry if this is confusing. It confuses me, too. I should have been a
      carpenter.

      - Dave

      Comment

      • Peter Hansen

        #4
        Re: translating PHP to Python

        Dave wrote:[color=blue]
        > The second point won't work, though, because by parent class I mean,
        > simply, the object that created the current object, *not* the class the
        > current class is based on.[/color]

        Good you clarified that, because "parent" definitely isn't used that way
        by most other people here. And, in fact, there's no requirement that an
        instance (object) be involved in creating a new object. Python allows
        functions that are not methods in a class. What would you expect to
        happen if a mere function was doing the creating?
        [color=blue]
        > So, for example:
        >
        > class A(object):
        > def __init__(self):
        > self.thing = Thing()
        > self.thing.meth ()
        >
        > def do_stuff(self):
        > print "Stuff"
        >
        > class Thing(object):
        > def meth(self):
        > #now here's what I WANT
        > self.parent.do_ stuff(args)
        >
        > Is there a built in way to do this in Python, or do I have to pass
        > "parent" when I init Thing?[/color]

        It's pretty much standard to pass in references to the caller, or
        perhaps even more standard to pass in a callable, often in the form of a
        a "bound method" when an object's method is doing the calling. On the
        other hand, what you are showing here is something that *would* normally
        be done with subclassing, and therefore with a parent class involved
        (using the conventional meaning of "parent").

        class A(object):
        def __init__(self):
        self.do_stuff()


        class Thing(A):
        def do_stuff(self):
        print "Stuff"


        But since this was a contrived example, I can't really tell what would
        work best for you in your real use case.

        -Peter

        Comment

        • Magnus Lycka

          #5
          Re: translating PHP to Python

          Dave wrote:[color=blue]
          > Is there a built in way to do this in Python, or do I have to pass
          > "parent" when I init Thing?[/color]

          While I'm sure you could find a "clever" way to do this, passing
          in "parent" explicitly is the "proper" way to do it. Once in a
          while, you might actually want some other object than the logical
          "parent" to do the object creation.

          By the way, you might want to google up "Law of Demeter". While
          many good Python programmers bend that rule a bit, it's a good
          aspect to have in mind when designing classes and programs.
          [color=blue]
          > Sorry if this is confusing. It confuses me, too. I should have been a
          > carpenter.[/color]

          I'm not so sure. Confused carpenters tend to do really stupid things
          like sawing off their fingers... Programming is a lot safer! ;^)

          Comment

          • Magnus Lycka

            #6
            Re: translating PHP to Python

            Peter Hansen wrote:[color=blue]
            > Good you clarified that, because "parent" definitely isn't used that way
            > by most other people here.[/color]
            Unless they are coding GUIs? I guess it's pretty common that GUI
            controls are contained in other controls called parents. At least
            that's how it's done in wxPython.

            Comment

            • Xavier Morel

              #7
              Re: translating PHP to Python

              Dave wrote:[color=blue]
              > Anyone familiar with PHP? I'm trying to make a translation. In PHP you
              > can get the current object's name by going like this:
              >
              > get_class(item) == 'ClassName'
              >
              > I've tried type(item), but since I can't be sure if I'll be in __main__
              > or as a child object, I can't guarantee what that value will return, so
              > I can't just manipulate that value as a string.
              >[/color]
              Type doesn't return a string, it returns a reference to a class object.

              You look like you want to test if the class of an object is <some
              specific class>. For that purpose, check isinstance.
              [color=blue]
              > Is there a simple way to get the current object's name? You would think
              > __name__ would work, right? It doesn't.
              >[/color]
              What do you call "the current object's name"? A python object usually
              has no name per se (only functions and classes do have one I think).
              [color=blue]
              > Now here's another, similar one:
              >
              > You can reference an object's parent object directly in PHP, like so:
              >
              > //note the charming use of semi-colon. isn't it cute?
              > parent::__const ruct(
              > $stuffInAWeirdS yntaxThatDoesnt MeanAnythingWhe nYouReadIt);
              >
              > I'd like to avoid passing a reference to an object's parent in
              > __init__, but is there a built in way in Python to say "You, Parent
              > Object, do ...stuff!"
              >
              > Thanks!
              >[/color]
              I guess it's a parent in the inheritance meaning of the term. If so, you
              can use either the call-by-class syntax or the `super` construct.

              For the call-by-class, see the Python tutorial, chapter 9.5
              "Inheritanc e", last paragraph. For the `super` construct, check the help
              on the subject, and the document "Unifying types and classes in Python
              2.2" by the BDFL (http://www.python.org/2.2.3/descrintro.html)

              Comment

              • Peter Hansen

                #8
                Re: translating PHP to Python

                Magnus Lycka wrote:[color=blue]
                > Peter Hansen wrote:[color=green]
                >>Good you clarified that, because "parent" definitely isn't used that way
                >>by most other people here.[/color]
                >
                > Unless they are coding GUIs? I guess it's pretty common that GUI
                > controls are contained in other controls called parents. At least
                > that's how it's done in wxPython.[/color]

                Entirely true... good point. I suppose I should say instead that "in
                the context of Object-Oriented Programming, the term 'parent class'
                almost universally refers to the superclass of a class, while the term
                'parent object' is ambiguous and might refer to the object from which
                another object is created provided a reference the creating object is
                passed in to the constructor." :-)

                (I notice the OP's original post refers to parent object, while his
                clarification refers to parent class... to add to the confusion.)

                -Peter

                Comment

                • Terry Hancock

                  #9
                  Re: translating PHP to Python

                  On Sun, 05 Feb 2006 15:04:32 -0500
                  Peter Hansen <peter@engcorp. com> wrote:
                  [color=blue]
                  > Dave wrote:[color=green]
                  > > The second point won't work, though, because by parent
                  > > class I mean, simply, the object that created the
                  > > current object, *not* the class the current class is
                  > > based on.[/color]
                  >
                  > Good you clarified that, because "parent" definitely isn't
                  > used that way by most other people here.[/color]

                  In Zope programming, for example, "parent" invariably means
                  neither the superclass nor a factory for the object, but
                  rather the container object holding the object (like a file
                  in a directory). This follows the conventional language for
                  talking about filesystem directory trees, of course.

                  Just to make sure you're really confused.

                  But if you're converting PHP to Python, it seems likely that
                  you will one day encounter Zope. My impression is that
                  people do things in PHP that are ordinarily split between
                  templates (ZPT or DTML) and Python "scripts" in Zope.

                  Of course, there are a dozen other ways to do
                  web-programming in Python, too.

                  Cheers,
                  Terry

                  --
                  Terry Hancock (hancock@Anansi Spaceworks.com)
                  Anansi Spaceworks http://www.AnansiSpaceworks.com

                  Comment

                  • Dave

                    #10
                    Re: translating PHP to Python

                    So thanks, all for the help.

                    Turns out that the solution is simple enough, as are most solutions in
                    Python:

                    PHP:
                    parent::__const ruct(args)

                    does translate to the Python:
                    super(ParentCla ss, self).__init__( args)

                    The example, that of referencing an object's creator object (if that's
                    the technospecifica list terminosity) has to be done by passing a
                    reference to the creator object to the created object.

                    So:

                    class A(object):
                    def create_child(se lf):
                    self.child = B()
                    self.child.do_s tuff(self)

                    class B(object):
                    def do_stuff(self, parent):
                    self.parent = parent
                    if self.parent.__c lass__.__name__ == 'A':
                    print "I'm a child of an A!"
                    else:
                    print "Well, I'm a motherless child. Does that mean I can
                    kill Macbeth?"

                    (Bonus points for lame, contrived, and sort of offensive Shakespeare
                    reference)

                    The project I'm working on is a CSS preprocessor. Watch this space.
                    It's totally going to be famous.

                    Comment

                    • Eric Nieuwland

                      #11
                      Re: translating PHP to Python

                      Dave wrote:[color=blue]
                      > class A(object):
                      > def create_child(se lf):
                      > self.child = B()
                      > self.child.do_s tuff(self)
                      >
                      > class B(object):
                      > def do_stuff(self, parent):
                      > self.parent = parent
                      > if self.parent.__c lass__.__name__ == 'A':
                      > print "I'm a child of an A!"
                      > else:
                      > print "Well, I'm a motherless child. Does that mean I can
                      > kill Macbeth?"[/color]

                      Depending on your actual needs you could change that to:

                      class A(object):
                      def create_child(se lf):
                      self.child = B(self)

                      class B(object):
                      def __init__(self, parent):
                      self.do_stuff(p arent)

                      def do_stuff(self, parent):
                      self.parent = parent
                      if self.parent.__c lass__.__name__ == 'A':
                      print "I'm a child of an A!"
                      else:
                      print "I know ye not!"

                      which IMHO makes it clearer from A's perspective.

                      --eric

                      Comment

                      Working...