translating PHP to Python

D

Dave

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::__construct(
$stuffInAWeirdSyntaxThatDoesntMeanAnythingWhenYouReadIt);

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!
 
F

Farshid Lashkari

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

className = item.__class__.__name__
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!"

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
 
X

Xavier Morel

Dave said:
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.
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.
Is there a simple way to get the current object's name? You would think
__name__ would work, right? It doesn't.
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).
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::__construct(
$stuffInAWeirdSyntaxThatDoesntMeanAnythingWhenYouReadIt);

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!
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
"Inheritance", 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)
 
D

Dave

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
 
P

Peter Hansen

Dave said:
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.

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?
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?

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
 
M

Magnus Lycka

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

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.
Sorry if this is confusing. It confuses me, too. I should have been a
carpenter.

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

Magnus Lycka

Peter said:
Good you clarified that, because "parent" definitely isn't used that way
by most other people here.
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.
 
P

Peter Hansen

Magnus said:
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.

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
 
T

Terry Hancock

Good you clarified that, because "parent" definitely isn't
used that way by most other people here.

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
 
D

Dave

So thanks, all for the help.

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

PHP:
parent::__construct(args)

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

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

So:

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

class B(object):
def do_stuff(self, parent):
self.parent = parent
if self.parent.__class__.__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.
 
E

Eric Nieuwland

Dave said:
class A(object):
def create_child(self):
self.child = B()
self.child.do_stuff(self)

class B(object):
def do_stuff(self, parent):
self.parent = parent
if self.parent.__class__.__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?"

Depending on your actual needs you could change that to:

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

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

def do_stuff(self, parent):
self.parent = parent
if self.parent.__class__.__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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top