首页 \ 问答 \ python class []函数(python class [] function)

python class []函数(python class [] function)

我最近从红宝石移动到Python和红宝石,你可以创建自我[nth]方法我怎么会在Python中做到这一点?

换句话说,你可以做到这一点

a = myclass.new
n = 0
a[n] = 'foo'
p a[n]  >> 'foo'

I recently moved from ruby to python and in ruby you could create self[nth] methods how would i do this in python?

in other words you could do this

a = myclass.new
n = 0
a[n] = 'foo'
p a[n]  >> 'foo'

原文:https://stackoverflow.com/questions/3680981
更新时间:2022-03-27 17:03

最满意答案

如果您打算使用调度程序,最好的解决方案是使用@PersistenceContext来获取EntityManager。 另一种选择是不使用会话范围的实体管理器(无论如何应该被视为不良实践),并使用默认的作用域或请求范围的实体管理器。

我说一个对话范围的实体管理器是不好的做法,因为它很容易导致延迟初始化问题,分离实体和内存泄漏。


If you're going to be using schedulers your best solution is to use @PersistenceContext to get an EntityManager. The other option is to not use a conversation scoped entitymanager (which should be considered bad practice anyway), and use a default scoped or request scoped entitymanager.

I say a conversation scoped entitymanager is bad practice because it can easily lead to lazy initialization issues, detached entities and memory leaks.

相关问答

更多
  • 你不需要一个容器:一个非常简短的教程可以在这里找到: http : //randling.wordpress.com/2011/08/15/cdi-in-java-se/ You don't need a container: A very short tutorial can be found here: http://randling.wordpress.com/2011/08/15/cdi-in-java-se/
  • 写CDI的人给了你一个大对象的工厂; 他们为你做了工作,比你更好。 它是XML配置或注释驱动的,所以你不必在代码中嵌入所有内容。 像春天这样的依赖注射发动机比你的工厂做得更多。 它将需要一个以上的工厂类和一行代码来复制他们提供的所有。 当然,你不必使用它。 你总是自由发明自己的轮子。 你应该 - 如果你的目的是学习如何制作轮子或消除依赖。 但是,如果你想开发应用程序,最好使用其他人提供的工具给他们一个优势。 关于依赖注入的重要文章是由Martin Fowler撰写的。 我建议阅读它; 八年后仍然很棒。 “还 ...
  • CDI代表“上下文和依赖注入”,而Spring是依赖注入容器的完整生态系统。 要比较两者,你必须区分比较。 依赖注入由两个容器处理。 主要区别在于CDI以动态 (又称:有状态)方式处理DI - 这意味着依赖关系在执行时解决。 Spring的方法是静态的 - 这意味着组件在创建时连接在一起。 虽然CDI方式看起来似乎有点不寻常,但它远远优越,并提供了更多和更高级的选项(我正在用两个有效的CDI应用程序的背景来编写它)。 如果你看生态系统 ,情况就会有所不同:春天来了很多罐子(> 150),而CDI本身也很小。 ...
  • 是的,您可以自由地混合CDI和EJB,并获得一些很好的结果。 听起来你正在使用@WebService和@Schedule ,这是将EJB添加到组合中的好理由。 这里有很多混乱,所以这里有一些关于EJB和CDI的一般信息,因为它们与每个相关。 EJB> = CDI 请注意,EJB 是 CDI bean,因此具有CDI的所有好处。 反之亦然(尚未)。 所以绝对不会陷入思考“EJB vs CDI”的习惯,因为逻辑真的转化为“EJB + CDI对CDI”,这是一个奇怪的方程式。 在将来的Java EE版本中,我们将 ...
  • 从Seam 2文档 : 嵌套对话具有自己的对话上下文,但可以从外部对话的上下文中读取值。 外部对话的上下文在嵌套对话中是只读的,但由于对象是通过引用获得的,因此对对象本身的更改将反映在外部上下文中。 因此,通过嵌套对话,您有机会将给定的父对话拆分为多个子对话,每个对话都包含其上下文和对父上下文的访问权限。 尽管CDI受到Seam的严重影响,但它只能说是几种影响的共同点,所以它并不包含Seam所拥有的所有内容。 我们的想法是,通过创建CDI扩展,例如Seam 3,以及Apache Deltaspike现在应该 ...
  • 请尝试以下方法: SomeBean bean = CDI.current().select(SomeBean.class).get(); 使用限定符? 请尝试以下方法: SomeBean bean = CDI.current().select(SomeBean, new AnnotationLiteral(){}, new Anno ...
  • 如果您打算使用调度程序,最好的解决方案是使用@PersistenceContext来获取EntityManager。 另一种选择是不使用会话范围的实体管理器(无论如何应该被视为不良实践),并使用默认的作用域或请求范围的实体管理器。 我说一个对话范围的实体管理器是不好的做法,因为它很容易导致延迟初始化问题,分离实体和内存泄漏。 If you're going to be using schedulers your best solution is to use @PersistenceContext to g ...
  • 第一个范围是请求的范围,所以为每个请求创建一个新的实例。 第二个是会话范围,所以为每个会话创建一个新会话。 CDI不会合并和回收对象,因为它不知道对象是否是有状态的,并且您不希望在请求中返回Bean在先前请求中的状态。 这会破坏请求/会话范围的整个点。 除非豆子的创建真的很昂贵(因为它们开始一个新的连接或类似的东西),集中它们不会带来任何好处。 时间短暂的物体现在非常快速地创建和收集垃圾。 如果这个bean真的很昂贵,那么它应该是一个单身人士。 The first one is scoped to the ...
  • 考虑到JSF2没有为FacesContext提供注入支持,似乎可能存在更大的问题。 我相信JSF 2.2通过@Inject增加了对此的支持。 无法注入托管属性,预期的等价物是为@Named bean提供正确定义的getter和setter,并从UI中利用它。 Considering that JSF2 provides you no injection support for FacesContext, it seems like there may be a bigger question at play ...
  • 如上所述@Boris Pavlović ,您可以通过编程方式引导Weld并获取bean。 但是,也可以使注射工作。 您需要在桌面应用程序中定义启动方法,该方法将“替换”您的public static void main(String ... args) 。 考虑: public class Main { @Inject private Bean bean; public void startup(@Observes ContainerInitialized event) { ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)