Python: call a function from string name

This question already has an answer here: Calling a function of a module by using its name (a string) 10 answers If it's in a class, you can use getattr: class MyClass(object): def install(self): print "In install" method_name = 'install' # set by the command line options my_cls = MyClass() method = None try: method = getattr(my_cls, method_name) except AttributeError

Python:从字符串名称中调用一个函数

这个问题在这里已经有了答案: 通过使用其名称(字符串)调用模块的功能10个答案 如果它在一个类中,则可以使用getattr: class MyClass(object): def install(self): print "In install" method_name = 'install' # set by the command line options my_cls = MyClass() method = None try: method = getattr(my_cls, method_name) except AttributeError: raise NotImplementedError("Class `{}`

object has no attribute

I am attempting to learn how to program. I really do want to learn how to program; I love the building and design aspect of it. However, in Java and Python, I have tried and failed with programs as they pertain to objects, classes, methods.. I am trying to develop some code for a program, but im stumped. I know this is a simple error. However I am lost! I am hoping someone can guide me to a

对象没有属性

我正在尝试学习如何编程。 我真的很想学习如何编程; 我喜欢它的建筑和设计方面。 然而,在Java和Python中,我尝试过程序,因为它们与对象,类,方法有关。我试图为程序开发一些代码,但是我很难过。 我知道这是一个简单的错误。 但是我迷路了! 我希望有人能指导我一个工作计划,但也帮助我学习(批评不仅是预期的,而且是赞美)。 class Converter: def cTOf(self, numFrom): numFrom = self.numFrom

how to call a function in a class from main

我正在尝试从main..how中调用一个类定义的函数吗? class file_process: def findb(self, id): .... def main(): id = sys.argv[2] file_process.findb(id)//how to call findb function from main,this is giving an error Since finddb is a method, you need to call it on an instance of file_process : file_process().finddb(id) I strongly urge you to study up on Python and classes, I can recommend

如何从main调用一个类的函数

我正在尝试从main..how中调用一个类定义的函数吗? class file_process: def findb(self, id): .... def main(): id = sys.argv[2] file_process.findb(id)//how to call findb function from main,this is giving an error 由于finddb是一种方法,因此您需要在file_process一个实例上调用它: file_process().finddb(id) 我强烈建议你学习Python和类,我可以推荐Python教程,然后继续。 您需要先创建一个类的实

'Self' of python vs 'this' of cpp/c#

我非常喜欢Python的OOP概念,所以我想知道Python的self功能与CPP / C# this关键字的功能类似。 self & this have the same purpose except that self must be received explicitly. Python is a dynamic language. So you can add members to your class. Using self explicitly let you define if you work in the local scope, instance scope or class scope. As in C++, you can pass the instance explicitly. I

python vs'this'的cpp / c#'Self'

我非常喜欢Python的OOP概念,所以我想知道Python的self功能与CPP / C# this关键字的功能类似。 除了必须明确地接收self之外, self和this具有相同的目的。 Python是一种动态语言。 所以你可以添加成员到你的班级。 明确使用self让您定义您是在本地作用域,实例作用域还是类作用域中工作。 和在C ++中一样,你可以明确地传递实例。 在下面的代码中, #1和#2实际上是相同的。 所以你可以使用方法作为正常的函数,没有歧义

Unbound method must be called with instance as first a

So, I have a class, where a class variable is set to the output from a class factory in the __init__ method, like so: def MyFooFactory(): def __init__(self, *args): # Do __init__ stuff def MyBar(myFoo_obj): print "MyBar" newclass = type("MyFoo", tuple(object), {"__init__": __init__, "MyBar": MyBar} ) return newclass class Foo: Bar = 0 def __init__(self)

必须以实例作为第一个a调用未绑定的方法

所以,我有一个类,其中类变量被设置为__init__方法中类工厂的输出,如下所示: def MyFooFactory(): def __init__(self, *args): # Do __init__ stuff def MyBar(myFoo_obj): print "MyBar" newclass = type("MyFoo", tuple(object), {"__init__": __init__, "MyBar": MyBar} ) return newclass class Foo: Bar = 0 def __init__(self): if (type(Foo.Bar) is int):

How to declare a static attribute in Python?

How can I declare a static attribute in Python? Here is written how I can declare a method: Static methods in Python? All variables defined on the class level in Python are considered static class Example: Variable = 2 # static variable print Example.Variable # prints 2 (static variable) # Access through an instance instance = Example() print instance.Variable # stil

如何在Python中声明静态属性?

我如何在Python中声明静态属性? 这里写我如何声明一个方法:Python中的静态方法? 在Python中的类级别上定义的所有变量都被认为是静态的 class Example: Variable = 2 # static variable print Example.Variable # prints 2 (static variable) # Access through an instance instance = Example() print instance.Variable # still 2 (ordinary variable) # Change within an instance instan

should I use static methods or top

I come from a Java background and I'm new to python. I have a couple scripts that share some helper functions unique to the application related to reading and writing files. Some functions associated with reading, some with writing. While searching for the correct approach, I saw this: Static methods in Python? He mentions in his answer: Finally, use staticmethod sparingly! There are

我应该使用静态方法还是使用top

我来自Java背景,我是python的新手。 我有几个脚本共享一些与读写文件相关的应用程序特有的辅助函数。 一些与阅读有关的功能,一些与写作有关。 在搜索正确的方法时,我看到了这个:Python中的静态方法? 他在回答中提到: 最后,谨慎使用静态方法! 有很少的情况需要在Python中使用静态方法,并且在单独的“顶层”函数更清晰的情况下,我已经看到它们被多次使用。 我不太了解顶层函数,我不确定给出这个更好的简单例子

Calling static method in python

I have a class Person and a static method in that class called call_person : class Person: def call_person(): print "hello person" In the python console I import the class Person and call Person.call_person() . But it is giving me error that says 'module' object has no attribute 'call_person' . Can anyone please let me know why I am getting this error? You need t

在python中调用静态方法

我有一个类Person和一个名为call_person类中的静态方法: class Person: def call_person(): print "hello person" 在python控制台中,我导入Person类并调用Person.call_person() 。 但它给了我错误,说'module' object has no attribute 'call_person' 。 任何人都可以让我知道为什么我得到这个错误? 您需要执行以下操作: class Person(object): #always inherit from object. It's just

Why does pycharm propose to change method to static

The new pycharm release (3.1.3 community edition) proposes to convert the methods that don't work with the current object's state to static. What is the practical reason for that? Some kind of micro-performance(-or-memory)-optimization? PyCharm "thinks" that you might have wanted to have a static method, but you forgot to declare it to be static. PyCharm proposes this bec

为什么pycharm建议将方法更改为static

新的pycharm版本(3.1.3社区版)建议将不适用的方法与当前对象的状态转换为静态。 这有什么实际的原因? 某种微型性能(或内存)优化? PyCharm“认为”你可能想要一个静态方法,但你忘记声明它是静态的。 PyCharm提出这一点是因为该方法在其主体中不使用self ,因此实际上并没有改变类实例。 因此,该方法可以是静态的,即可以调用而不需要以前创建类实例。 同意@jolvi,@AndndasR和其他人,警告发生在不使用self的成员

Imitating a static method

This question already has an answer here: Static methods in Python? 8 answers You should use @classmethod : @classmethod def show(cls, message): print("The message is: {}".format(message)) The difference between a classmethod and a staticmethod is that the latter knows nothing about its enclosing class, whereas the former does (via the cls argument). A staticmethod can just as eas

模仿静态方法

这个问题在这里已经有了答案: Python中的静态方法? 8个答案 你应该使用@classmethod : @classmethod def show(cls, message): print("The message is: {}".format(message)) classmethod和staticmethod之间的区别在于后者对它的封闭类一无所知,而前者不会(通过cls参数)。 一种staticmethod可以在课堂外轻松宣布。 如果你不想让show()知道关于C任何信息,可以在C之外使用@staticmethod或声明show() 。