473,473 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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!

Feb 5 '06 #1
10 1440
> 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
Feb 5 '06 #2
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

Feb 5 '06 #3
Dave wrote:
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

Feb 5 '06 #4
Dave wrote:
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! ;^)
Feb 5 '06 #5
Peter Hansen wrote:
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.
Feb 5 '06 #6
Dave wrote:
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(
$stuffInAWeirdSyntaxThatDoesntMeanAnythingWhenYouR eadIt);

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)
Feb 6 '06 #7
Magnus Lycka wrote:
Peter Hansen wrote:
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.


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

Feb 6 '06 #8
On Sun, 05 Feb 2006 15:04:32 -0500
Peter Hansen <pe***@engcorp.com> wrote:
Dave wrote:
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.


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 (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Feb 6 '06 #9
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.

Feb 6 '06 #10
Dave wrote:
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

Feb 6 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Davis Marques | last post by:
hi; I'm translating some PHP scripts to Python and have hit a roadblock with a for statement. If someone could explain to me how one should translate the multiple increment, evaluations, etc....
2
by: Henrik S. Hansen | last post by:
How do you best go about translating characters like '\\n' to '\n'? This is for a configuration file parser, where the "backslash convention" is supported. The naive approach --...
13
by: cjl | last post by:
Hey all: I'm working on a 'pure' python port of some existing software. Implementations of what I'm trying to accomplish are available (open source) in C++ and in Java. Which would be...
2
by: calin.hanchevici | last post by:
Hi all, I have a C++ library I call from python. The problem is I have c++ exceptions that i want to be translated to python. I want to be able to do stuff like: try: my_cpp_function() except...
5
by: Vyz | last post by:
Hi, I have a script with hundreds of lines of javascript spread accross 7 files. Is there any tool out there to automatically or semi-automatically translate the code into python. Thanks Vyz
9
by: Daniel Gee | last post by:
A while ago I wrote a class in Java for all kinds of dice rolling methods, as many sides as you want, as many dice as you want, only count values above or below some number in the total, things...
1
by: Rolf Wester | last post by:
Hi, is there a tool to automatically translate Matlab source to Python/numpy code? Regards Rolf
1
by: trihaitran | last post by:
Hi I am new to Ruby from Python and am still learning some stuff. I have a Ruby program that uses the mechanize library to connect to a Web site and authenticate with a username and password. The...
15
by: hofer | last post by:
Hi, Let's take following perl code snippet: %myhash=( one =1 , two =2 , three =3 ); ($v1,$v2,$v3) = @myhash{qw(one two two)}; # <-- line of interest print "$v1\n$v2\n$v2\n"; How...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.