将PHP翻译成Python [英] translating PHP to Python

查看:109
本文介绍了将PHP翻译成Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何熟悉PHP的人?我正在尝试翻译。在PHP中,你可以通过以下方式获得当前对象的名称:


get_class(item)==''ClassName''


我尝试过类型(项目),但因为我不能确定我是否会在__main__

或作为子对象,我不能保证这个价值会返回什么,所以

我不能把这个值当作一个字符串来操纵。


有没有获取当前对象名称的简单方法?你会认为

__name__会起作用,对吗?它不是。


现在这里是另一个,类似的:


你可以引用一个对象的父对象直接在PHP中,如下:


//注意分号的迷人用法。不是很可爱吗?

parent :: __ construct(

$ stuffInAWeirdSyntaxThatDoesntMeanAnythingWhenYouR eadIt);


我想要为了避免在

__init__中传递对象父元素的引用,但在Python中是否有内置的方式来说你,父母

对象,做......东西!


谢谢!

解决方案

stuffInAWeirdSyntaxThatDoesntMeanAnythingWhenYouReadIt);


我想避免在

__init__中传递对对象父级的引用,但在Python中是否存在内置方式说你,父母

对象,做......东西!


谢谢!


>有一种简单的方法来获取当前对象的名称吗?您会认为

__name__会起作用,对吧?它不是。


className = item .__ class __.__ name__

我想避免在
__init__中传递对象父母的引用,但是在Python中是否有内置的方式说你,父对象,做......东西!




使用超级()函数访问父类的属性


类C(B):

def meth(self,arg):

super(C,self).meth(arg)


-Farshid


Farshid,


这是一个很好的帮助,谢谢。


第二点不会工作,因为我的意思是父母班级,

简单地说,创建当前对象的对象,*不是*当前类所基于的类。


所以,例如:


A级(对象):

def __init __(自我):

self.thing = Thing()

self.thing.meth()


def do_stuff(个体经营):

print" S tuff"


class Thing(object):

def meth(self):

#now here'是我的意思想要

self.parent.do_stuff(args)


是否有内置的方法在Python中执行此操作,或者我必须传递

" parent"我什么时候开始做什么?


如果这令人困惑,很抱歉。它也让我感到困惑。我应该是一个

木匠。


- 戴夫


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!

解决方案

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!


> 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


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


这篇关于将PHP翻译成Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆