寻找方法多次包含来自anotherpython代码的一些.py代码 [英] looking for way to include many times some .py code from anotherpython code

查看:63
本文介绍了寻找方法多次包含来自anotherpython代码的一些.py代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在寻找一些简单的方法来做一些像c或PHP中包含的东西。

想象一下我想拥有:

cat somefile.py

a = 222

b = 111

c = 9

cat somefile2。 py

self.xxx = a

self.zzz = b

self.c = c

self。 d = d

cat anotherfile.py

def a():

包括somefile

后处理(a)


def b():

包括somefile

后处理(a,b,c)


class klass():

def __init __(self,a,b,c,d):

包括somefile2


我知道模块导入和重新加载,但我不确定这是否是正确的

方式。主要是,我想为多个对象实例分配一些自我绑定的
变量。他们的价值观会有所不同,所以我不能使用全局变量。


Martin

解决方案

马丁MOKREJ?写道:


我正在寻找一些简单的方法来做一些像c或PHP中包含的东西。
想象一下我想拥有:....

我知道模块导入和重新加载,但我不确定这是否是正确的方法。主要是,我想分配给多个对象实例
一些自限变量。他们的价值观会有所不同,所以我不能使用全局变量。




毫无疑问,有人会找到一种方法来编码。我建议你在这里用语言对抗
- 学会用它代替。决定你真正想要做什么,而不是你想做什么。然后尝试在正常的语言流程中找出如何实现真正目标的方法。

-Scott David Daniels
Sc *********** @ Acm.Org


Martin MOKREJ?写道:


我正在寻找一些简单的方法来做一些像c或PHP中包含的东西。
想象一下我想要:

cat somefile.py
a = 222
b = 111
c = 9

cat somefile2.py
self.xxx = a
self.zzz = b
self.c = c
self.d = d

cat anotherfile.py

def a( ):
包含somefile
后处理(一)

def b():
包含somefile
后处理(a,b,c)

类klass():
def __init __(self,a,b,c,d):
包含somefile2


你可以这样做使用模块级变量和klass的基类:


cat somefile.py

a = 222

b = 111

c = 9

cat somefile2.py

类别基础:

def __init __(self,a,b,c, d):

self.xxx = a

self.zzz = b

self.c = c

self.d = d

cat anotherfile .py

导入somefile,somefile2

def a():

postprocess(somefile.a)


def b():

postprocess(somefile.a,somefile.b,somefile.c)


class klass(s​​omefile2 .base):

def __init __(self,a,b,c,d):

somefile2.base .__ init __(self,a,b,c,d)


Kent


我知道模块导入和重装,但我不确定这是否正确

要走的路。主要是,我想为多个对象实例分配一些自我绑定的变量。他们的价值观会有所不同,所以我不能使用全局变量。

Martin



Scott David Daniels写道:< blockquote class =post_quotes> Martin MOKREJ?写道:


我正在寻找一些简单的方法来做一些像c或PHP中包含的东西。
想象一下,我希望: ....

我知道模块导入和重新加载,但我不确定这是否是正确的方法。主要是,我想为多个对象
实例分配一些自限变量。他们的价值观会有所不同,所以我不能


>使用全局变量。



毫无疑问,有人会找到一种方法来编写代码。我建议你在这里对抗语言 - 学会用它代替。决定你真正想要做什么,而不是你想做什么。然后尝试在正常的语言流程中找出如何实现真正目标。




请参阅我在3月2日发表的关于自动分配的帖子类变量" ;.

我没有答案,也许我不够清楚...... :(


我需要定义很多变量变量名通常是相同的。

问题是,如果我将这样的代码放入函数中......不,我不会通过

来通过什么东西到一个函数让它返回。我只想得到

分配了很多变量。如果我把它们放入模块中,它就会得到'

除非我重新加载,否则只执行一次。而且我必须使用:

来自某些导入*,因为主要是我对分配给自己感兴趣:

self.x =" blah"

self.y =" uhm"


我是新手,当然。


M.


Hi,
I''m looking for some easy way to do something like include in c or PHP.
Imagine I would like to have:
cat somefile.py
a = 222
b = 111
c = 9
cat somefile2.py
self.xxx = a
self.zzz = b
self.c = c
self.d = d
cat anotherfile.py

def a():
include somefile
postprocess(a)

def b():
include somefile
postprocess(a, b, c)

class klass():
def __init__(self, a, b, c, d):
include somefile2

I know about module imports and reloads, but am not sure if this is the right
way to go. Mainly, I want to assign to multiple object instances some self bound
variables. Their values will be different, so I can''t use global variables.

Martin

解决方案

Martin MOKREJ? wrote:

Hi,
I''m looking for some easy way to do something like include in c or PHP.
Imagine I would like to have: ....

I know about module imports and reloads, but am not sure if this is the
right way to go. Mainly, I want to assign to multiple object instances
some self bound variables. Their values will be different, so I can''t
use global variables.



Someone will, no doubt, find a way to code this. I suggest you are
fighting the language here -- learn to use it instead. Decide what
you really want to do, not how you want to do it. Then try to figure
out how to accomplish your real goal in the normal flow of the language.
-Scott David Daniels
Sc***********@Acm.Org


Martin MOKREJ? wrote:

Hi,
I''m looking for some easy way to do something like include in c or PHP.
Imagine I would like to have:
cat somefile.py
a = 222
b = 111
c = 9
cat somefile2.py
self.xxx = a
self.zzz = b
self.c = c
self.d = d
cat anotherfile.py

def a():
include somefile
postprocess(a)

def b():
include somefile
postprocess(a, b, c)

class klass():
def __init__(self, a, b, c, d):
include somefile2
You can do this with module-level variables and a base class for klass:

cat somefile.py
a = 222
b = 111
c = 9
cat somefile2.py
class base:
def __init__(self, a, b, c, d):
self.xxx = a
self.zzz = b
self.c = c
self.d = d
cat anotherfile.py
import somefile, somefile2

def a():
postprocess(somefile.a)

def b():
postprocess(somefile.a, somefile.b, somefile.c)

class klass(somefile2.base):
def __init__(self, a, b, c, d):
somefile2.base.__init__(self, a, b, c, d)

Kent


I know about module imports and reloads, but am not sure if this is the
right
way to go. Mainly, I want to assign to multiple object instances some
self bound
variables. Their values will be different, so I can''t use global variables.

Martin



Scott David Daniels wrote:

Martin MOKREJ? wrote:

Hi,
I''m looking for some easy way to do something like include in c or PHP.
Imagine I would like to have: ....

I know about module imports and reloads, but am not sure if this is
the right way to go. Mainly, I want to assign to multiple object
instances some self bound variables. Their values will be different,
so I can''t


> use global variables.



Someone will, no doubt, find a way to code this. I suggest you are
fighting the language here -- learn to use it instead. Decide what
you really want to do, not how you want to do it. Then try to figure
out how to accomplish your real goal in the normal flow of the language.



See my post on Mar 2 about "automating assignment of class variables".
I got no answers, maybe I wasn''t clear enough ... :(

I need to define lots of variables. The variable names are often identical.
The problem is that if I put such a code into a function ... no, I''m not going
to pass anything to a function to get it returned back. I just want to get
lots of variables assigned, that all. If I put them into module, it get''s
exectued only once unless I do reload. And I''d have to use:
"from some import *", because mainly I''m interrested in assigning to self:
self.x = "blah"
self.y = "uhm"

I''m newbie, sure.

M.


这篇关于寻找方法多次包含来自anotherpython代码的一些.py代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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